Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js listenTo window resize投掷[object object]没有方法';应用';错误_Backbone.js_Backbone Events - Fatal编程技术网

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”);
谢谢!采用活动空间是个好主意。