[英]Scoping issue with PhoneGap and Sencha touch
I'm having issues getting some scoping right with the below app.我在使用以下应用程序确定范围时遇到问题。 by the time I call this.fireEvent I'm no longer scoped in my app, but rather the document window.
到我调用 this.fireEvent 时,我的应用程序中不再有范围,而是文档 window。
Any ideas?有任何想法吗?
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); } });
Found an answer Bah, I worked at this for a couple hours before posting then figure it out 8 minutes later.找到答案呸,我在发布之前工作了几个小时,然后在 8 分钟后弄清楚了。 I tried using
DN.App.fireEvent
but that also didn't work.我尝试使用
DN.App.fireEvent
但这也没有用。 I realized then that DN.App was just the class I made, not the actual object.然后我意识到 DN.App 只是我制作的 class,而不是实际的 object。 In another file I call it as
DNApp = new DN.App();
在另一个文件中,我将其称为
DNApp = new DN.App();
After seeing that I tried DNApp.fireEvent
and it worked great.看到后我尝试
DNApp.fireEvent
并且效果很好。
The problem is "document.addEventListener("backbutton", this.backKey, true);", when the event is fired is expected the "this" keyword to be bond to the object which fired it, you could achieve what you want with a closure like:问题是“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.