Converting a normal module into an AMD module

To convert a normal module, we will perform the following steps. Always apply the same steps for all our modules. We need to wrap them into the define function, list dependencies, and return the module we returned in our old one.

  1. Open the viewmodel.js file.
  2. Create the define function:
    define([],function(){});
  3. Add all dependencies:
    define([
      'knockout',
      'models/Product',
      'models/Customer',
      'models/CartProduct',
      'services/ProductService',
      'services/CartProductService',
      'resources/ProductResource',
      'resources/OrderResource'
    ],function (ko, Product, Customer, ProductService, CartProductService, ProductResource, OrderResource) {
    });
  4. Export the module into the define function:
    define([],function(){ var debug = ko.observable(false); ...

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.