2013-11-20 4 views
1

Мне нужно вызвать функцию в ItemController/или установить новое свойство дочернего элемента ItemController из ArrayController.Как получить доступ к ItemController от ArrayController

Для лучшей картины

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

Я не нашел способ сделать это.

ответ

0

Проблема лежит на «замечает». «Контент» изменяется до того, как модели будут правильно свернуты в 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(); 
    }); 
} 
1

Это должно быть для каждого экземпляра arrayController. this.get("content") здесь возвращает массив моделей, которые не обернутый в itemController

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

Nope, то «this.forEach» не перебирать subControllers вообще. – Yman

+0

Хорошо, исправление, вы правы, что «this.forEach» возвращает itemController. Я узнал, что проблема на самом деле «наблюдает». Я поставлю правильный способ ждать решения ниже. В любом случае, спасибо! Дал +1 – Yman

Смежные вопросы