2013-11-20 28 views
1

我需要調用ItemController中的函數/或從ArrayController中設置子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 
    } 
}); 

我還沒有找到任何辦法做到這一點。

回答

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

這應該是forEach on arrayController實例本身。 this.get("content")此處返回未包裹在itemController中的模型陣列

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

不,在「this.forEach」不通過子控制器迭代的。 – Yman

+0

好的,正確的,「this.forEach」返回itemController。我發現問題實際上是在「觀察」。我將發佈一個正確的方式來等待下面的解決方案。不管怎樣,謝謝你!給你一個+1 – Yman

相關問題