簡體   English   中英

PhoneGap 和 Sencha touch 的范圍問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM