我正在尝试使用 on()
和 un()
函数(addListener
和 的别名)在运行时添加和删除监听器>删除监听器
)。
我从使用 Sencha 的 getCmp()
Ext.Viewport.on('orientationchange', Ext.getCmp('foo').handleOrientationChange, foo, {buffer: 50});
Ext.Viewport.un('orientationchange', Ext.getCmp('foo').handleOrientationChange);
来自 sencha 文档:
un( eventName, fn, [scope], [options], [order] )
The handler to remove. This must be a reference to the function passed into the addListener call.
我不清楚为什么这不起作用。显然,第二个函数引用与第一个函数引用不匹配,但为什么会发生这种情况呢? Ext.getCmp('foo')
不应该返回相同的对象吗?
最佳答案
调用 removeListener
时,您还需要传递 addListener
中指定的相同作用域,然后仅删除监听器
Ext.Viewport.un('orientationchange', Ext.getCmp('foo').handleOrientationChange, foo);
scope : Object (optional)
The scope originally specified for the handler. It must be the same as the scope argument specified in the original call to addListener or the listener will not be removed.
检查:
关于javascript - Ext.mixin.Observable.on()/.un() : function reference not matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929464/