【问题标题】:How to update content of one ArrayController from the selected value of another ArrayController in Ember.js如何从 Ember.js 中另一个 ArrayController 的选定值更新一个 ArrayController 的内容
【发布时间】:2012-06-10 16:11:48
【问题描述】:

我在 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
});

我宁愿解决这个问题,而无需 parentsController 了解其他控制器的任何信息。这应该可以通过观察者、绑定甚至通过计算来实现,但我不知道从哪里开始。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript arrays binding ember.js observers


    【解决方案1】:

    您可以使用绑定系统。 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')
    });
    

    还有here is the jsfiddle associated

    注意: 你也可以直接绑定选中的属性:

    App.sonsController = Em.ArrayController.create({
        parentSelectedBinding: 'App.parentsController.selected',
          ...
    
        updateContent: function() {
           ...
        }.observes('parentSelected')
    })
    

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2012-09-14
      • 1970-01-01
      相关资源
      最近更新 更多