Backbone.js listenTo window resize投掷[object object]没有方法';应用';错误
问题:Backbone.js listenTo window resize投掷[object object]没有方法';应用';错误,backbone.js,backbone-events,Backbone.js,Backbone Events,问题: this.listenTo($(window),"resize", this.resizeContext, this)); resizeContext: function(event) { console.log("resizing context for "+this.id); this.setHeight(); // trigger resize event (use event bus)
this.listenTo($(window),"resize", this.resizeContext, this));
resizeContext: function(event) {
console.log("resizing context for "+this.id);
this.setHeight();
// trigger resize event (use event bus)
this.options.vent.trigger("resize", event);
}
我正在尝试使用Backbone.js中的new listenTo()方法从视图将调整大小事件附加到窗口。事件似乎绑定到窗口,但是,当实际调整窗口大小时,会引发以下错误:
未捕获的TypeError:对象[Object Object]没有方法“apply”
js:2p.event.dispatch
js:2p.event.add.g.handle.h
以下是将事件附加到视图的代码:
this.listenTo($(window),"resize", this.resizeContext, this));
resizeContext: function(event) {
console.log("resizing context for "+this.id);
this.setHeight();
// trigger resize event (use event bus)
this.options.vent.trigger("resize", event);
}
这是resizeContext函数:
this.listenTo($(window),"resize", this.resizeContext, this));
resizeContext: function(event) {
console.log("resizing context for "+this.id);
this.setHeight();
// trigger resize event (use event bus)
this.options.vent.trigger("resize", event);
}
注意:使用标准的
$(窗口).on(“resize”,this.resizeContext)
附加事件并按其应该的方式运行。我正在尝试利用新的stopListening()
功能,该功能添加到view.remove()
新的listenTo
和stopListening
是主干.Events
混合的方法,它们只能用于侦听由触发的主干事件,例如内置的集合:add
或模型:change
事件
这意味着您将无法对DOM事件(如window:resize
)使用stopListening
功能
考虑重写视图。请删除方法
var SomeView = Backbone.View.extend({
initialize:function() {
$(window).on("resize",this.resizeContext)
},
remove: function() {
$(window).off("resize",this.resizeContext);
//call the superclass remove method
Backbone.View.prototype.remove.apply(this, arguments);
}
});
如果要继续使用listenTo
,可能需要为DOM元素使用以下一次性包装器:
/**
* Use Backbone Events listenTo/stopListening with any DOM element
*
* @param {DOM Element}
* @return {Backbone Events style object}
**/
function asEvents(el) {
var args;
return {
on: function(event, handler) {
if (args) throw new Error("this is one off wrapper");
el.addEventListener(event, handler, false);
args = [event, handler];
},
off: function() {
el.removeEventListener.apply(el, args);
}
};
}
例如:
view.listenTo(asEvents(window), "resize", handler);
并且侦听器将在view.remove()
或view.stoplistening()上自动删除
下面是多事件侦听器的更复杂的实现在我的代码中,我需要这样做
.debounce((this.resizeContext).bind(this))
这使得它更难被关闭。作为一个肮脏的解决方案,我只是在删除视图时关闭所有“调整大小”侦听器。
我猜在新视图中,如果有任何调整大小的侦听器,它将再次打开
remove: function() {
$(window).off("resize");
//call the superclass remove method
Backbone.View.prototype.remove.apply(this, arguments);
}
非常感谢。我注意到您正在使用.off()
删除事件。我使用unbind()来删除它们,但是,即使在调用view.remove()
和view.unbind()
之后,它们仍在启动。unbind()
是否只删除主干事件,我是否应该使用off()
?@Zenginer,命名有点混乱:主干对象(如视图)有on/off
和bind/unbind
两种方法,它们只是同一事物的两个名称:(un)绑定主干事件。jQuery还有一个名为on/off
的方法,它是一个bind/unbind
方法,用于(取消)绑定jQuery(DOM)事件。在这两种情况下,您都应该使用on/off
变体,因为bind/unbind
已被弃用。我现在看到了曙光。谢谢请记住,如果要在.on中使用.bind(someObj),例如:$(window).on(“resize”,this.resizeContext.bind(this)),.bind()将创建新函数,并且不会解除绑定。您可以首先使用.bind()定义函数,如:this.resizeContext=function(){…}.bind(someObj),也可以使用事件命名,如:$(window).on(“resize.uniqueName”,this.resizeContext.bind(someObj)),$(window).off(“resize.uniqueName”);真的很喜欢它,感觉更像是一个很好的解决方案!不过还有一个补充,添加上下文作为参数以获得正确的绑定。首先on:function(event,handler,context){
el.addEventListener(event,handler.bind(context),false);
.1.2.1主干网目前使用的是internalOn
,因此这不再有效。如果您想使用取消公告版本的处理程序并保持其他“调整大小”事件处理程序不变,请使用jquery名称空间绑定/取消绑定事件,如$(窗口)。打开(“resize.myspace”、u.debounce(…);$(窗口)。关闭(“resize.myspace”);
谢谢!采用活动空间是个好主意。