[英]Scoping issue with PhoneGap and Sencha touch
我在使用以下应用程序确定范围时遇到问题。 到我调用 this.fireEvent 时,我的应用程序中不再有范围,而是文档 window。
有任何想法吗?
DN.App = Ext.extend(Ext.Panel, { initComponent: function() { document.addEventListener("backbutton", this.backKey, true); DN.App.superclass.initComponent.call(this); }, setActivePanel: function(){ //global handler for switching panels }, backKey: function(){ var prev = DN.App.prevPanel[DN.App.prevPanel.length-1]; DN.App.prevPanel.pop(); //This handles the switching of panels, but 'this' is scoped to html document at this point this.fireEvent('setActivePanel',prev); } });
找到答案呸,我在发布之前工作了几个小时,然后在 8 分钟后弄清楚了。 我尝试使用DN.App.fireEvent
但这也没有用。 然后我意识到 DN.App 只是我制作的 class,而不是实际的 object。 在另一个文件中,我将其称为DNApp = new DN.App();
看到后我尝试DNApp.fireEvent
并且效果很好。
问题是“document.addEventListener("backbutton", this.backKey, true);",当事件被触发时,预计“this”关键字将绑定到触发它的 object,你可以实现你想要的像这样的闭包:
document.addEventListener("backbutton", (function(self){ return function(){ self.backKey(); }; })(this), true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.