0
我有三個字段quantity
,price
和total
。從Meteor collection的數量和價格更新總數
我只更新quantity
和price
,所以total
應自動計算。
我如何確保total
始終正確更新?我想我應該使用集合鉤子。
我有三個字段quantity
,price
和total
。從Meteor collection的數量和價格更新總數
我只更新quantity
和price
,所以total
應自動計算。
我如何確保total
始終正確更新?我想我應該使用集合鉤子。
如果您正在使用自動窗體和簡單模式,只需使用一個autovalue
'price': { type: Number },
'quantity': { type: Number },
'total': {
type: Number,
autoValue: function() {
const price = this.field('price');
const quantity = this.field('quantity');
if (price.isSet && quantity.isSet) {
if (this.isInsert) {
return quantity.value * price.value;
} else {
return { $set: quantity.value * price.value };
}
}
}
}
是'total'只是'quantity' *'price',抑或是基於其他您的收藏的東西?你如何做更新(允許/拒絕VS方法)? –
我正在通過'type =「update」'autoform更新數量和價格。 – Jamgreen
最簡單的解決方案不是存儲'total'。當你需要時總是計算它。 – JeremyK