2016-03-02 26 views
0

在ArcGIS JS API中,當擴展盤更改完成後,我需要觸發我的類的方法。我沒有爲此找到任何特殊事件。當數據尚未加載時,extent-change觸發得太早。 update-end也是由我想調用的函數觸發的,使得無限循環。所以我需要的是把兩個事件聯繫在一起,第二個只是一次。但是如何?如何鏈接Dojo中的「extent-change」和「update-end」事件?

我的大多數事件是這樣的:

this.events.push(on(this.map, "extent-change", lang.hitch(this, this._foo))); 

這不適合於事件聯繫起來,所以我需要鏈接的情況下一些其他的方式。我已經試過:

_foo: function() { 
    function fooEvent() { 
     this._bar(); 
     dojo.disconnect(updateHandle); 
    } 
    var updateHandle = dojo.connect(map, "onUpdateEnd", fooEvent()); 
} 

_bar是我想要的程度變化結束時運行的方法。然而,事件處理程序中的this意味着別的東西,而不是包含該功能的類。我也嘗試了我在declare聲明中聲明的類名,但沒有運氣。

_foo()_bar()屬於同一類(我們稱之爲"foobar/baz")。然而,fooEvent()裏面是不是它的子類,因爲我希望 - 當我嘗試在它使用this.inherited,這是不確定的。我嘗試的另一種方法是將event參數添加到處理程序,但它也是未定義的。所以除非有更好的方法,否則我需要了解如何獲得"foobar/baz"類的對象。

我嘗試另一種方法是使用lang.hitch一次,通過以下方式之一:

//through the cluster event 
var updateHandle = dojo.connect(map, "onUpdateEnd", lang.hitch(this, clusterEvent)); 

//or directly calling _bar() 
var updateHandle = dojo.connect(map, "onUpdateEnd", { langh:lang.hitch(this, this._bar), disc:dojo.disconnect(updateHandle)}); 

//or through on, leaving a rouge event listener 
dojo.on(map, "onUpdateEnd", lang.hitch(this, this._bar)); 

他們沒有返回任何明確的錯誤,雖然_bar()方法似乎工作一段時間,這不是現在不工作 - 對於之前的三次都是如此。我不明白這些聽衆是做什麼的。

+0

您的意思是方法_foo:在類A中聲明的方法和類B中的_bar()方法例如? –

+0

@bRIMOsBor:都在同一個班級。我通常只是用它來引用其他方法和屬性。 –

回答

0

我通過展開事件偵聽器解決了這個問題。首先,我做了一個標誌_reloadFlag,在構造函數初始化false後又改爲true每當我想在_foo調用_bar,等等。 _bar現在開始用_reloadFlag要麼將​​其設置爲false或返回任何測試。事件監聽器現在看起來像這樣:

this.events.push(on(this.map, "extent-change", lang.hitch(this, this._foo))); 
this.events.push(on(this.map, "update-end", lang.hitch(this, this._bar)));