如何从ArrayController访问ItemController

How to access ItemController from ArrayController

本文关键字:访问 ItemController ArrayController      更新时间:2023-09-26

我需要调用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
    }
});

我没有找到任何方法来做这件事。

应该是arrayController实例本身上的forEach。这里的this.get("content")返回未封装在itemController 中的模型数组

this.forEach(function(item) {
  item.doSomething(); 
});

问题在于"观察"。在模型被正确包装在itemController(在本例中是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();
    });
}