在骨干网中我想扩展Backbone.View.prototype.delegateEvents
function (events) {
if (!(events || (events = getValue(this, 'events')))) return;
this.undelegateEvents();
for (var key in events) {
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
var match = key.match(eventSplitter);
var eventName = match[1], selector = match[2];
method = _.bind(method, this);
eventName += '.delegateEvents' + this.cid;
if (selector === '') {
this.$el.bind(eventName, method);
} else {
this.$el.delegate(selector, eventName, method);
}
}
}
在其中添加其他功能。它是一种扩展原型而不是完全覆盖原型的方法吗?
就我而言,我正在尝试访问events
arg。
编辑:我试图实现与此相关的内容
function (events) {
if (events['rightclick .element']){ App.doStuff() }
if (!(events || (events = getValue(this, 'events')))) return;
this.undelegateEvents();
for (var key in events) {
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) throw new Error('Event "' + events[key] + '" does not exist');
var match = key.match(eventSplitter);
var eventName = match[1], selector = match[2];
method = _.bind(method, this);
eventName += '.delegateEvents' + this.cid;
if (selector === '') {
this.$el.bind(eventName, method);
} else {
this.$el.delegate(selector, eventName, method);
}
}
}
答案 0 :(得分:1)
如果你刚才想要一些东西,那么你可以这样做:
var delegateEvents = Backbone.View.prototype.delegateEvents;
Backbone.View.prototype.delegateEvents = function(events) {
// You have to do this here because delegateEvents rarely
// gets the events from its argument, they almost always
// come from this.events.
if(!(events || (events = _(this).result('events'))))
return;
// Now do your special stuff.
if(events['rightclick .element'])
App.doStuff();
// And chain it off to the original implementation. We'll
// always supply the events argument here since we've already
// done the "is it passed or in this?" logic up above.
delegateEvents.call(this, events);
};
请注意,_.result
与旧getValue
内部函数的作用相同。
演示:http://jsfiddle.net/ambiguous/25Xh9/
此外,我知道没有rightclick
事件,但如果您需要,则there are ways to detect one。