2012-03-19 28 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

相关问题