2012-03-19 30 views
1

我試圖將我的應用程序遷移到使用Ember-Data作爲持久性機制。有一點讓我感到震驚的是,我不確定是否仍有可能使用arrayProxy作爲hasMany關聯的聚合屬性。在我之前的迭代中,我沒有任何明確的關聯,只是控制器被特定的屬性綁定在一起。現在我想利用ember-data中的關聯功能,但是當我將數組代理的內容綁定到DS.Model的「children」屬性時,我遇到了錯誤。我的代碼的下方,這裏有一個的jsfiddle:http://jsfiddle.net/sohara/7p6gb/22/如何使用Ember-Data關聯與ArrayProxy

我得到的錯誤是:

Uncaught TypeError: Object App.recipeController.content.ingredients has no method 'addArrayObserver' 

我希望能夠保留控制器層,即使數據關聯是在controlleed模型級別。它也(理想情況下)將子對象嵌入到父對象的json表示中,以避免多個服務器請求。

window.App = Ember.Application.create(); 

App.store = DS.Store.create({ 
    revision: 3, 
    adapter: DS.fixtureAdapter 
}); 

App.Ingredient = DS.Model.extend({ 
    name: DS.attr('string'), 
    price: DS.attr('string') 
}); 

App.Recipe = DS.Model.extend({ 
    name: DS.attr('string'), 
    ingredients: DS.hasMany('App.Ingredient', {embedded: true}) 
}); 

App.Recipe.FIXTURES = [ 
    {id: 1, name: 'Pizza', ingredients: [{id: 1, name: 'tomato sauce', price: 2, recipeId: 1}]} 
]; 

App.recipeController = Ember.Object.create({ 
    content: App.store.find(App.Recipe, 1) 
}); 

App.ingredientsController = Ember.ArrayProxy.create({ 
    content: 'App.recipeController.content.ingredients', 

    totalWeigth: function() { 
     var price = 0; 
     items = this.get('content'); 
     items.forEach(function(item) { 
      weight += price; 
     }); 
    }.property() 

}); 

回答

4

App.ingredientsController你需要有contentBinding: 'App.recipeController.content.ingredients',而不是content: ...

+0

你說得對。謝謝!我以爲我也嘗試過,但我想我做錯了什麼。 – 2012-03-19 23:16:54

相關問題