0
我有一個加載所有訂單項的路線。每個訂單項目都有一個產品。在控制器中,我想通過遍歷所有訂單項並將order-item.quantity
乘以product.price
來計算訂單總額。但是,order.get('product.price')
不會返回價格。我認爲這是因爲這種關係是異步的。 total
計算屬性的結果是NaN
。我該如何解決?謝謝!如何從控制器訪問Ember Data中相關模型的數據?
我還創建了一個JSBin http://emberjs.jsbin.com/lisedavela/edit?html,js,output
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('order-item');
}
});
App.IndexController = Ember.Controller.extend({
total: function() {
return this.get('model').reduce(function(total, order) {
return total + order.get('product.price');
}, 0);
}.property('[email protected]')
});
App.OrderItem = DS.Model.extend({
quantity: DS.attr('number'),
product: DS.belongsTo('product', { async: true })
});
App.Product = DS.Model.extend({
title: DS.attr('string'),
price: DS.attr('number')
});
App.Product.reopenClass({
FIXTURES: [
{ id: 100, title: 'Product A', price: 10 },
{ id: 200, title: 'Product B', price: 20 },
{ id: 300, title: 'Product C', price: 30 }
]
});
App.OrderItem.reopenClass({
FIXTURES: [
{ id: 1, quantity: 5, product: 100 },
{ id: 2, quantity: 2, product: 200 },
{ id: 3, quantity: 1, product: 300 }
]
});