保存を兼ね:エンバーコンピュープロパティは、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機能(簡体字):私はテンプレートにshipmentPrice
を印刷しようとすると、
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']);
}
);
}
そして最後に、それは更新されません。サーバーから価格が返されていて、shipment_line
のpackage_price
が設定されています。
また、shipment.save();
の貨物を保存してルートを表示ページにリダイレクトすると、ページを更新するまで価格が倍増します。ページの更新後、すべてが正しく表示されます。
最初の質問は、どのように新しい出荷ラインオブジェクトを追加する際に、計算されたプロパティのshipmentPriceを更新するのですか?それとも不可能なのでしょうか?
2番目の質問は、保存後に価格が倍増するのはなぜですか。
申し訳ありませんが、遅れてご返答いただきましたが、テストする機会があり、結果も同じです。 – pusle