如何从ArrayController访问ItemController

时间:2013-11-20 12:58:52

标签: javascript ember.js

我需要在ItemController中调用一个函数/或者从ArrayController设置子ItemController的新属性。

为了更好的图片

App.ProductsController = Ember.ArrayController.extend({
    needs: ["product"],
    itemController: 'product',

    contentChanged: function() { //no idea how to initiate this, hence I use observes

        // How do I access the children controllers from here?
        // I had tried below, but not working:
        // this.get("content").forEach(function(item) {
        //      item.doSomething(); 
        // });

    }.observes('content')

});

App.ProductController = Ember.ObjectController.extend({
    doSomething: function() {
       //supposed to do something
    }
});

我还没有找到任何办法。

2 个答案:

答案 0 :(得分:1)

这应该是arrayController实例本身的forEach。 this.get("content")此处返回未包含在itemController

中的模型数组
this.forEach(function(item) {
  item.doSomething(); 
});

答案 1 :(得分:0)

问题在于“观察”。在contentController中正确包装模型之前,“内容”会发生变化(在本例中为App.ProductController)。

因此,观察变化的正确方法是覆盖

//in App.ProductsController (aka Parent Controller)
arrayContentDidChange: function(startIdx, removeAmt, addAmt) {
    this._super(startIdx, removeAmt, addAmt);

    //then look for the children as such:
    this.forEach(function(item){
         item.doSomething();
    });
}