我想创建一个页面,在左侧我有固定的视图(一些过滤器),它们适用于右侧的结果。如何在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