I am working on a nested backbone view, in which if you click on, it will create a new instance of the same view. I want to disable only a specific event, not all of them; in this case, the click. I tried using undelegateEvents(), but this will disable all the functions. Any ideas on how can this be done?
Here is a piece of the code I am working on:
var View = Backbone.View.extend({
events: {
"mousedown": "start",
"mouseup": "over"
},
start: function() {
var model = this.model;
var v = new View({
model: model,
});
v.undelegateEvents(); //I just want to disable mousedown
v.render();
},
over: function() {
/*
some code here
*/
},
render: function() {
/*
some code here
*/
}
});
The idea is to ban clicking in the second instantiated view while keeping the other events. The first one will have all of its events.
Thanks
You can specify the events you want to use when you call
delegateEvents:So you could do something like this:
You might want to push that logic into a method on View though:
You need the
this.events = _.clone(this.events)trickery to avoid accidentally altering the “class’s”events(i.e. this.constructor.prototype.events) when you only want to change it for just one object. You could also have a flag for theViewconstructor that would do similar things inside itsinitialize:Another option would be to have a base view without the
mousedownhandler and then extend that to a view that does have themousedownhandler:You’d have to duplicate the
B.eventsinsideVor mess around with a manualextendon theeventsas_.extendwon’t merge the properties, it just replaces things wholesale.