2014-01-08 41 views
4

如何從不屬於RouteController的JavaScript函數內轉換Ember.js應用程序?我可以直接使用Ember.Application實例調用類似route.transitionTo()controller.transitionToRoute()的東西嗎?從JavaScript轉換Ember.js應用程序

我想要做的是將amMap集成到Ember.js應用程序中,這樣當點擊地圖上的某個區域時,應用程序就會轉換到適當的狀態。

Here's an example jsFiddle。當從列表中選擇一個大陸時,在地圖上選擇該大陸。我想反也發生了,我該怎麼辦下面的聽衆:

map.addListener("homeButtonClicked", function() { 
    // how can I transition to index from here? 
    // similar to this.transitionTo("index") from a route or 
    // this.transitionToRoute("index") from a controller 
    alert("Transition to index"); 
}); 
map.addListener("clickMapObject", function (e) { 
    // how can I transition to continent from here? 
    // similar to this.transitionTo("continent", id) from a route or 
    // this.transitionToRoute("continent", id) from a controller 
    alert("Transition to continent: " + e.mapObject.id); 
}); 
+0

我的答案是否適合你?如果是這樣,我會欣賞賞金。 :) – Ben

回答

3

啊,但爲什麼當你可以有更多的樂趣全局函數做到這一點做的「餘燼方式」 :)

您最好將地圖div移動到模板中,並創建一個「IndexView」,並在didInsertElement()方法中爲地圖視圖添加事件處理程序。這是一個工作的emberjsbin:http://emberjs.jsbin.com/EsAkAVE/1/edit

+1

謝謝,這幫助我朝着解決方案。我最終將地圖包裝在一個組件中,該組件在單擊區域時觸發一個動作,並從控制器處理該動作。作爲獎勵,我沒有地圖的全局變量了。 – imgx64

相關問題