February 2015
Intermediate to advanced
232 pages
4h 40m
English
If you look in the models/product.js file, you can see that the model contains some logic:
var hasStock = function () {
return _product.stock() > 0;
};
var decreaseStock = function () {
var s = _product.stock();
if (s > 0) {
s--;
}
_product.stock(s);
};We are going to move this logic and some more to a service with the following steps:
services.ProductService.hasStock and decreaseStock functions, as follows:var ProductService = (function() {
var hasStock = function (product) {
return product.stock() > 0;
};
var decreaseStock = function (product) {
var s = product.stock();
if (s > 0) {
s--;
}
product.stock(s);
}; return { hasStock:hasStock, ...Read now
Unlock full access