Ember 在 hasMany 关系上计算 属性 未更新且在保存时加倍
Ember Computed Property on hasMany relationship not updating and doubles on save
我有两个模型,一个与另一个有很多关系:
装运:
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('shipment_lines.@each.package_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: Ember.computed('shipment_lines.[]', function () {
let price = 0;
this.get('shipment_lines').forEach(function(shipment_line) {
price += Number(shipment_line.get('package_price'));
});
return price;
}),
我终于弄明白了。好吧,至少我找到了解决方法。保存模型后,我手动将 shipmentPrice
重置为 0
:
freight_order.save().then(
(new_freight_order) => {
shipment.set('shipmentPrice', 0);
this.transitionToRoute('shipments.show', new_freight_order.id);
}
);
我想服务器响应由于某种原因被添加到计算值中。
我有两个模型,一个与另一个有很多关系:
装运:
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('shipment_lines.@each.package_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: Ember.computed('shipment_lines.[]', function () {
let price = 0;
this.get('shipment_lines').forEach(function(shipment_line) {
price += Number(shipment_line.get('package_price'));
});
return price;
}),
我终于弄明白了。好吧,至少我找到了解决方法。保存模型后,我手动将 shipmentPrice
重置为 0
:
freight_order.save().then(
(new_freight_order) => {
shipment.set('shipmentPrice', 0);
this.transitionToRoute('shipments.show', new_freight_order.id);
}
);
我想服务器响应由于某种原因被添加到计算值中。