简体   繁体   English

绑定后未触发调整大小事件

[英]Resize event not firing after binding

I'm using bind on bind on jQuery to click and resize listeners with unique namespace - so I can unbind them safely. 我在jQuery上使用bind on bind来单击并调整具有唯一名称空间的侦听器的大小,因此可以安全地取消绑定它们。

When pressing click - it is logging the "Mouse Clicked" message. 按下click时-它记录“ Mouse Clicked”消息。

But when changing the window size - nothing happans. 但是,当更改窗口大小时,不会发生任何事情。

class MyView {
    constructor() {
        $(window.document).on('click.myView', this._onDocumentClick.bind(this));
        $(window.document).on('resize.myView', this._onDocumentResize.bind(this));
        $('#clean').on('click', this._cleanup.bind(this));
    }
    _cleanup() {
        $(window.document).unbind('resize.myView');
        $(window.document).unbind('click.myView');
    }
    _onDocumentResize() {
        console.log('Window Resized'); //Not reaching here
    }
    _onDocumentClick() {
        console.log('Mouse Clicked'); //OK
    }
}

Any ideas what is the difference between the resize and click event? 有什么想法可以resizeclick事件之间的区别吗?

That's because the resize event is fired by the window object, not the window.document object :) therefore, this should work: 这是因为resize事件是由window对象而不是window.document对象:)触发的,因此,这应该可以工作:

$(window).bind('resize.myView', this._onWindowResize.bind(this));

p/s: In your initial event handler binding, you seem to have swapped the callback and the events, ie resize triggering the private click method, and vice versa. p / s:在初始事件处理程序绑定中,您似乎已经交换了回调和事件,即resize触发私有单击方法,反之亦然。

Even better: don't use .bind() anymore, newer versions of jQuery recommends using .on() , ie: 更好的是:不再使用.bind() ,较新版本的jQuery建议使用.on() ,即:

$(window).on('resize.myView', this._onWindowResize.bind(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM