Creating the CartProduct service
The cart item service also extracts the logic of the CartProduct
model. To create this service, follow these steps:
Create a file called
CartProductService.js
in theservice
folder.Remove the
addUnit
andremoveUnit
methods from theCartProduct
model.Update the service with these methods:
var CartProductService = (function() { var addUnit = function (cartItem) { var u = cartItem.units(); var _stock = cartItem.product.stock(); if (_stock === 0) { return; } cartItem.units(u+1); cartItem.product.stock(--_stock); }; var removeUnit = function (cartItem) { var u = cartItem.units(); var _stock = cartItem.product.stock(); if (u === 0) { return; } cartItem.units(u-1); cartItem.product.stock(++_stock); }; return { addUnit:addUnit, removeUnit:removeUnit }; })();