我想創建一個頁面,在左側我有固定的視圖(一些過濾器),它們適用於右側的結果。如何在Ember.js中的控制器之間進行通信
例如,左側是根據流派,標題,創建年份過濾電影的過濾器。 右側是不同的圖表和表格,根據所選過濾器進行更新。
所以我想在左邊有一個固定的視圖,然後在右邊的一個根據路線變化的插座。
這是正確的方法嗎?如果是這樣,我無法弄清楚如何將過濾器更改傳遞給右側的控制器。
這的jsfiddle顯示了類似的設置:http://jsfiddle.net/DEHcK/2/
在ContentRoute - > setupController我得到NavigationController的實例,但我無法弄清楚如何讓改變someValue中。
在上面的示例中,ContentController如何獲取NavigationController中someValue的更改?
這是實現我在ember中描述的應用程序的正確方法嗎?
的JavaScript:
window.App = Ember.Application.create();
App.Router.map(function() {
this.route('content');
});
App.ContentRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('navigationController', this.controllerFor('navigation'));
}
});
App.ContentController = Ember.ObjectController.extend({
navigationController: null,
observerOfSomeValue: function() {
this.set('observedValue', this.get('navigationController.someValue'));
}.observes('navigationController', 'navigationController.someValue'),
observedValue: null
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('content');
}
});
App.NavigationView = Ember.View.extend({
init: function() {
this._super();
this.set('controller', this.get('parentView.controller').controllerFor('Navigation'));
},
templateName: "navigation"
});
App.NavigationController = Ember.ObjectController.extend({
someValue: 'xx',
observedValue: null,
observerOfSomeValue: function() {
this.set('observedValue', this.someValue);
}.observes('someValue')
});
HTML:
<script type="text/x-handlebars" data-template-name="application" >
<div>
{{view App.NavigationView}}
</div>
<div>
{{ outlet }}
</div>
</script>
<script type="text/x-handlebars" data-template-name="navigation" >
Change {{view Ember.TextField valueBinding="controller.someValue"}}
<div>observed value in same view: {{controller.observedValue}}</div>
</script>
<script type="text/x-handlebars" data-template-name="content" >
<div style="margin-top: 2em">
observed value in another view: {{observedValue}}
</div>
</script>
感謝@Wildhoney,這是一個更新的小提琴。 http://jsfiddle.net/DEHcK/3/ – 2013-02-14 10:36:07