1.

How Can You Attach Listeners To Events In A View?

Answer»

USE the “events” attribute of Backbone.View. Remember that event listeners can only be attached to CHILD elements of the “el” PROPERTY.

See example below:

SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var template = _.template( $("#search_template").html(), {} );
this.$el.html( template );
},
//Attach listener to click event of the search BUTTON
events: {
"click INPUT[type=button]": "doSearch"
},
doSearch: function( event ){
// Button clicked, you can access the element that was clicked with event.currentTarget
alert( "Search for " + $("#search_input").val() );
}
});

Use the “events” attribute of Backbone.View. Remember that event listeners can only be attached to child elements of the “el” property.

See example below:

SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var template = _.template( $("#search_template").html(), {} );
this.$el.html( template );
},
//Attach listener to click event of the search button
events: {
"click input[type=button]": "doSearch"
},
doSearch: function( event ){
// Button clicked, you can access the element that was clicked with event.currentTarget
alert( "Search for " + $("#search_input").val() );
}
});



Discussion

No Comment Found