cod*_*ger 6 javascript arrays binding observers ember.js
我在ember.js中遇到以下问题.子控制器依赖于父控制器中的选定值来确定其内容.在数据库中,子项具有parent_id引用.
App.parentsController = Em.ArrayController.create({
content: [],
selected: null
});
App.sonsController = Em.ArrayController.create({
// the value of content depends on the id of
// the selected item in the parentsController
content: [],
selected: null
});
App.daughtersController = Em.ArrayController.create({
// the value of content depends on the id of
// the selected item in the parentsController
content: [],
selected: null
});
Run Code Online (Sandbox Code Playgroud)
我更愿意在没有parentController必须知道其他控制器的情况下解决这个问题.这应该可以通过观察者,绑定甚至通过计算实现,但我不知道从哪里开始.任何帮助将不胜感激.
您可以使用绑定系统.在sonsController需要观察parentsController.selected的属性,然后更新其内容.
以下是如何执行此操作的示例:
App.parentsController = Em.ArrayController.create({
content: [],
selected: null
});
App.sonsController = Em.ArrayController.create({
parentControllerBinding: 'App.parentsController',
content: [],
updateContent: function() {
var selected = this.getPath('parentController.selected');
var newContent = Ember.A();
newContent.pushObject(selected);
this.set('content', newContent);
}.observes('parentController.selected')
});
Run Code Online (Sandbox Code Playgroud)
注意:您也可以直接绑定选定的属性:
App.sonsController = Em.ArrayController.create({
parentSelectedBinding: 'App.parentsController.selected',
...
updateContent: function() {
...
}.observes('parentSelected')
})
Run Code Online (Sandbox Code Playgroud)