I am currently working on BackboneJS and am kinda new to it.
I have this view called "product" which has the usual - product.tpl, product_app.js, product_controller.js and product_view.js. Apart from these I also have a commonJS.js, which as the name suggests contains data / functions common to all views.
I am currently binding events in the views using - "eventname selector" : "functionname", in the events block of every view.
The issue is that there are some keypress/keydown events which I would like to bind in the commonJS so as to not write the same code over and over in all the views.
events:{
"keydown .number-only": "isNumeric",
"keydown .text-only": "isText",
"keydown .alphanumeric": "isAlphanumeric"
},
Is there any way to do the same?
Backbone accepts a method as the value of events
property.
You can extend your common event data with the custom event hash for the view and return it:
events: function() {
// access commonEventHash from your common file
return _.extend(commonEventHash, {
"click .custom": "customMethod" // events specific to view
});
}