Yman
Yman

Reputation: 934

How to access ItemController from ArrayController

I need to invoke a function within ItemController / or set new property of the child ItemController from ArrayController.

For better picture

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

I have not found any way to do this.

Upvotes: 1

Views: 195

Answers (2)

Yman
Yman

Reputation: 934

The problem lies on "observes". The "content" changes way before the models are being wrapped correctly in the itemController (in this case is App.ProductController).

Hence the correct way to observe changes is to override the

//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();
    });
}

Upvotes: 0

Yury Svechinsky
Yury Svechinsky

Reputation: 336

That should be forEach on arrayController instance itself. this.get("content") here returns array of models, that are not wrapped in itemController

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

Upvotes: 1

Related Questions