2015-11-19 47 views
0

我有三個字段quantitypricetotal從Meteor collection的數量和價格更新總數

我只更新quantityprice,所以total應自動計算。

我如何確保total始終正確更新?我想我應該使用集合鉤子。

+0

是'total'只是'quantity' *'price',抑或是基於其他您的收藏的東西?你如何做更新(允許/拒絕VS方法)? –

+0

我正在通過'type =「update」'autoform更新數量和價格。 – Jamgreen

+0

最簡單的解決方案不是存儲'total'。當你需要時總是計算它。 – JeremyK

回答

2

如果您正在使用自動窗體和簡單模式,只需使用一個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 }; 
     } 
    } 
    } 
}