我有兩種型號,一種用的hasMany關係其他:灰燼計算屬性上的hasMany關係不更新和雙打上保存
裝運:
import DS from 'ember-data';
export default DS.Model.extend({
pickup_address: DS.belongsTo('address', { inverse: null }),
delivery_address: DS.belongsTo('address', { inverse: null }),
shipment_lines: DS.hasMany('shipment-line', { inverse: null }),
shipmentPrice: Ember.computed('[email protected]_price', function() {
let price = 0;
this.get('shipment_lines').forEach(function(shipment_line) {
price += Number(shipment_line.get('package_price'));
});
return price;
}),
});
而且貨物線模型:
import DS from 'ember-data';
export default DS.Model.extend({
description: DS.attr('string'),
package_weight: DS.attr('number'),
package_length: DS.attr('number'),
package_width: DS.attr('number'),
package_height: DS.attr('number'),
package_price: DS.attr('number'),
});
在控制器中,當我向發貨添加新的裝運行時,我從API獲取包裝的價格並將其添加到裝運行:
addShipmentLine(shipmentLine) {
// Get price for shipment line
this.getShipmentLinePrice(shipmentLine);
// Add shipment line to shipment
let shipment = this.get('shipment');
shipment.get('shipment_lines').addObject(shipmentLine);
},
的getShipmentLinePrice功能(簡體):
getShipmentLinePrice(shipmentLine) {
...
// Get weight and type
let weight = shipmentLine.get('package_weight');
let length = shipmentLine.get('package_length');
let height = shipmentLine.get('package_height');
let width = shipmentLine.get('package_width');
// Fetch price from API
this.get('price').getPackagePrice(weight, length, height, width).then(
(price) => {
shipmentLine.set('package_price', price['price']);
}
);
}
最後,當我嘗試打印shipmentPrice
模板,它不會更新。即使價格從服務器返回,並且設置了shipment_line
s package_price
。
此外,當我使用shipment.save();
保存貨件並將路線重定向到展示頁面時,價格翻倍,直到我刷新頁面。刷新頁面後,所有內容都能正確顯示。
所以第一個問題是,如何在添加新的裝運行對象時獲得裝運計算屬性shipmentPrice以更新?還是不可能?
第二個問題是,爲什麼保存後價格翻了一番?
對不起,遲到的回覆,剛剛有機會測試它,並給出了相同的結果。 – pusle