loopback/example/client-server/models.js

36 lines
768 B
JavaScript
Raw Normal View History

2014-02-19 19:44:16 +00:00
var loopback = require('../../');
2014-02-20 01:09:36 +00:00
var CartItem = exports.CartItem = loopback.DataModel.extend('CartItem', {
2014-02-19 19:44:16 +00:00
tax: {type: Number, default: 0.1},
price: Number,
item: String,
qty: {type: Number, default: 0},
cartId: Number
});
CartItem.sum = function(cartId, callback) {
this.find({where: {cartId: 1}}, function(err, items) {
var total = items
.map(function(item) {
return item.total();
})
.reduce(function(cur, prev) {
return prev + cur;
}, 0);
callback(null, total);
});
}
loopback.remoteMethod(
CartItem.sum,
{
accepts: {arg: 'cartId', type: 'number'},
returns: {arg: 'total', type: 'number'}
}
);
CartItem.prototype.total = function() {
return this.price * this.qty * 1 + this.tax;
}