Created
December 3, 2015 22:57
-
-
Save zeckdude/924ed29afdb3d433ea5f to your computer and use it in GitHub Desktop.
Help with Backbone Views Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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