簡體   English   中英

Prototype.js回調未觸發

[英]Prototype.js callback not firing

我正在嘗試為Garmin DeviceControl瀏覽器插件創建一個Google Earth控制器。 我正在對現有的Google MAPS控制器進行行為建模,但是遇到的問題是第二個回調函數沒有被觸發。

編碼:

Garmin.GoogleEarthMapController = function(mapString){}; //just here for jsdoc
Garmin.GoogleEarthMapController = Class.create();
Garmin.GoogleEarthMapController.prototype = {
    initialize: function (mapString) {
        google.setOnLoadCallback(this.GEinit);
    },
    GEinit: function() {
            alert("call create");
            //This is a Google.js function and works when executed outside of prototype
            google.earth.createInstance(this.mapElementName, this.GEinitCallback, this.GEfailureCallback);
            alert("finished create instance");
    },
    GEinitCallback: function (instance) {
                alert("init called");
    },

    GEfailureCallback:function (errorCode) {
    }
}

所有必需的js都包含在HTML中,它們都有效並且目標div存在。 在我的原型js類之外運行代碼也會產生所需的結果。 我的原型類中發生的事情是GEinitCallback從未執行。 我收到“調用創建”警報和“完成創建實例”警報,但從不出現“被調用初始化”警報。 我沒有JavaScript錯誤。

我是否正確指定了回調? 如果沒有,我該怎么辦? google.earth.createInstance函數在Google Earth源文件中定義,並且在控制器外部。

您的作用域存在問題-每次您遇到回調時,都有可能會松開this的定義。 嘗試使用bind()的回調定義什么this是在功能方面。 我注意到了下面的電話。 您還可以簡化類定義以及以下內容

Garmin.GoogleEarthMapController = Class.create({
    initialize: function (mapString) {
        // make sure you bind this call
        google.setOnLoadCallback(this.GEinit.bind(this));
    },
    GEinit: function() {
        alert("call create");
        //This is a Google.js function and works when executed outside of prototype
        //make sure you bind this call
        google.earth.createInstance(this.mapElementName, this.GEinitCallback.bind(this), this.GEfailureCallback.bind(this));
        alert("finished create instance");
    },
    GEinitCallback: function (instance) {
            alert("init called");
    },

    GEfailureCallback:function (errorCode) {
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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