Skip to content

Instantly share code, notes, and snippets.

@zeckdude
Created December 3, 2015 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeckdude/924ed29afdb3d433ea5f to your computer and use it in GitHub Desktop.
Save zeckdude/924ed29afdb3d433ea5f to your computer and use it in GitHub Desktop.
Help with Backbone Views Events
// Handling events in a View
var SongView = Backbone.View.extend({
events: {
"click button": "onClick", // Run the "onClick" function when the any button is clicked
"click button.bookmark": "onClickBookmark", // Run the "onClickBookmark" function when the a button with the class "bookmark" is clicked
"click input[type=button]": "doSomething" // Shows that you can include the same types of selectors as in jQuery
},
onClick: function() {
console.log("Listen clicked");
},
onClickBookmark: function(e) {
e.stopPropagation(); // Stops the click event on the clicked element to propogate(or move on) to any other handlers that apply to it, so it only runs this function
console.log("Bookmark Clicked");
},
render: function() {
this.$el.html(this.model.get("title") + " <button>Listen</button> <button class='bookmark'>Bookmark</button>");
return this;
}
});
var Song = Backbone.Model.extend();
var song = new Song({ title: "Blue in Green" });
var songView = new SongView({ el: "body", model: song });
songView.render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment