Hi there!
First of all I used MeteorKitchen schema and I try to use it more on my own - change things etc.
I have a little problem with my Javascript code after editing the table.
I have a form and a table in it. It has a description, quantity and a price columns. With a quantity and price my app counts the amount.?
I could edit my table and when I click “edit” → my Amount in other rows is changing to actual (former ?) value of actually edited row. Description, quantity and price don’t change - only the Amount.
Extra: After Ad. 4 and looking into Robomongo I realized, that it changes always to the value of my last item in database collection.
I would be grateful if you could look at it and help me with that
-
I add the first product
-
Then I add second product
Now it looks like that:
-
So I want to change something in “a” row. That’s the effect. As you can see in “b” row the Amount column change to “a”'s amount value.
-
I add the third product
-
And I edited now the “b” row. The effect is the same:
When I want to change other values - it’s OK. The only problem is that “Amount” column
My collection on server side looks like this (Don’t be scared, MeteorKitchen magic ):
InvoicesItem.allow({
insert: function (userId, doc) {
return InvoicesItem.userCanInsert(userId, doc);
},
update: function (userId, doc, fields, modifier) {
return InvoicesItem.userCanUpdate(userId, doc);
},
remove: function (userId, doc) {
return InvoicesItem.userCanRemove(userId, doc);
}
});
InvoicesItem.before.insert(function(userId, doc) {
doc.createdAt = new Date();
doc.createdBy = userId;
doc.modifiedAt = doc.createdAt;
doc.modifiedBy = doc.createdBy;
if(!doc.ownerId) doc.ownerId = userId;
doc.amount = doc.quantity*doc.price;
});
InvoicesItem.before.update(function(userId, doc, fieldNames, modifier, options) {
modifier.$set = modifier.$set || {};
modifier.$set.modifiedAt = new Date();
modifier.$set.modifiedBy = userId;
if(!modifier.$set) return; var quantity = modifier.$set.quantity || doc.quantity; var price = modifier.$set.price || doc.price; modifier.$set.amount = quantity*price;
});
InvoicesItem.before.remove(function(userId, doc) {
});
InvoicesItem.after.insert(function(userId, doc) {
var sum = 0; InvoicesItem.find({ invoiceId: doc.invoiceId }).map(function(item) { sum += item.amount; }); Invoices.update({ _id: doc.invoiceId }, { $set: { totalAmount: sum }});
});
InvoicesItem.after.update(function(userId, doc, fieldNames, modifier, options) {
var sum = 0; InvoicesItem.find({ invoiceId: doc.invoiceId }).map(function(item) { sum += item.amount; }); Invoices.update({ _id: doc.invoiceId }, { $set: { totalAmount: sum }});
});
InvoicesItem.after.remove(function(userId, doc) {
var sum = 0; InvoicesItem.find({ invoiceId: doc.invoiceId }).map(function(item) { sum += item.amount; }); Invoices.update({ _id: doc.invoiceId }, { $set: { totalAmount: sum }});
});