javascript - Ext.mixin.Observable.on()/.un() : function reference not matching

标签 javascript extjs sencha-touch-2 listener

我正在尝试使用 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.

检查:

Sencha Docs

关于javascript - Ext.mixin.Observable.on()/.un() : function reference not matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929464/

相关文章:

sencha-touch-2 - 在水平列表中显示多个项目

css - datepickerfield sencha touch 2 - 如何将默认占位符颜色从黑色更改为灰色?

javascript - 在 sencha touch 2 中滚动面板时出现文本字段焦点问题

javascript - 有没有办法删除 JSFiddle 嵌入式 View 中的顶部栏

javascript - Android 上的 Chrome 无法加载我的页面

Extjs 类系统静态

javascript - 基于 ExtJS 的 Web 应用程序的 UI 测试框架和工具

javascript - 如何将动态图例添加到 Apex 中的 D3 力定向图?

javascript - 如何仅在单击选项卡时加载 DIV?

Extjs4,如何在组合编辑器中设置displayField?