我有一個組件代表地圖,並且在我的控制器中的一個操作之後,我想調用組件上的方法來使地圖居中。該代碼看起來像這樣如何從控制器調用組件的方法
App.PlacesController = Ember.Controller.extend({
actions : {
centerMap : function() {
// how to call from here to GoogleMapComponent.centerMap ??
}
}
});
App.GoogleMapComponent = Ember.Component.extend({
centerMap : function() {
}
});
模板
{{google-map}}
<button {{action "centerMap"}}>Center Map</button>
我已經找到了一個解決辦法,但我不認爲這是這樣做的灰燼方式。
{{google-map viewName="mapView"}}
<button class="center-map">Center Map</button>
App.PlacesView = Ember.View.extend({
didInsertElement : function() {
this.$(".center-map").click(this.clickCenterMap.bind(this));
},
clickCenterMap : function() {
this.get("mapView").centerMap();
}
});
我使用Ember 1.1.2並且EmberController沒有觸發方法。 這是正確的模式?控制器應該啓動事件並且視圖聽取它們? – axelhzf
@axelhzf要觸發事件並偵聽它們,您需要通過添加'Ember.Evented'混合來創建一個對象,如下所示:'App.someController = Ember.Controller.extend(Ember.Evented,{...}) ;',那麼你可以使用'Ember.Evented'混合中的方法來訂閱事件並引發事件。 –
我喜歡這個解決方案,但是我之前沒有看到$ .proxy的使用 - 你能澄清一下到底做了什麼嗎? –