The Services module

Services are also singletons, like resources, so follow the same steps as the resources module:

  1. Open the services/ProductService.js file.
  2. Create the top hierarchy module:
    var Shop;
    Shop = Shop || {};
  3. Create the Resources namespace:
    Shop.Services = Shop.Services || {};
  4. Define ProductService:
    Shop.Services.ProductService = (function(){
    })();
  5. In this case the service has no dependencies.
  6. Finally, set the following code in the services/ProductService.js file. Since in the application the resources are singleton, extend the resource with the methods used in the following code:
    var Shop; Shop = Shop || {}; Shop.Services = Shop.Services || {}; Shop.Services.ProductService = (function(Product) { var hasStock = function (product) { return product.stock() ...

Get KnockoutJS Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.