Git Product home page Git Product logo

backbone-views-events's Introduction

Intro to Backbone Events

Overview

  • About
  • Instructions
  • Example
  • Events
  • Resources

About

One of the jobs of Backbone views is to respond to events or user interaction. Each view is responsible for a section of the user interface, and any user interaction that happens in that section should be handled by the view. Its job is to listen for events and then call a function to update the model with any changes. Backbone works seamlesly with jQuery to add and remove listeners to the section of the DOM that it manages. The listeners are actually attached to the el property of the view, or the root element of the view.

In this lab you will build a view that can respond to a click event and add that view to the DOM.

Instructions

  1. Create a view object in views/main.js
    • You will do this the same way you did in backbone-basic-views.
    • In addition to initialize and render the view should also have an events hash which calls #handleClick on click.
    • Look at the below example and read the Events section below to see how the events hash should be structured.
  2. In app.js you should instantiate your view object and append the HTML string to the DOM.

Example

The events property of the view object looks like this:

MyView = Backbone.View.extend({
  events: {
    "click .icon":          "open",
    "click .button.edit":   "openEditDialog",
   }
  open : function() {
    console.log('open!');
  },
  openEditDialog : function(e) {
    // Backbone will pass us the jQuery event object
    console.log('Opening Edit Dialog!');
  }
})

###Events

  1. Each key in the events hash mirrors the event type it listens for like click, submit, mouseenter, followed by a space and then a css selector to scope where exactly the event listener should be listening within all the HTML managed by the view.
  2. The value is a string that is the name of a function defined on the view.

In this lab you are going to add some HTML to the page using a Backbone view. We're going to add a clickable element ("a link"), and we're going to stop the default behavior of the link and then simply write to the console.

Resources

View Intro to Backbone Events on Learn.co and start learning to code for free.

backbone-views-events's People

Contributors

benjagross avatar blake41 avatar kthffmn avatar sarogers avatar stephaniecoleman avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone-views-events's Issues

A few problems I see here

In the instructions for what to do on the lab (or in the actual specs etc) we need more clarity on what we expect them to do. It's non obvious that in the bootstrap function they should instantiate a view and attach it to the DOM. Maybe we even record a video of the finished product and what it should look like?
We should probably remind them they should have an initialize method, they should call render in it.
Let's also add a test to make sure they log something to the console and prevent default on the event object (not exactly sure how to test that last part so ignore it if it's too difficult)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.