Book Image

KnockoutJS Essentials

By : Jorge Ferrando, Jorge F Ferrando
Book Image

KnockoutJS Essentials

By: Jorge Ferrando, Jorge F Ferrando

Overview of this book

Table of Contents (16 chapters)
KnockoutJS Essentials
About the Author
About the Reviewers

The Models module

As we have done with the view-model, we are going to convert each model into a component and we are going to wrap it inside a module called Models, the following steps:

  1. Open the models/product.js file.

  2. Define our top-level module, Shop, and initialize it:

    var Shop;
    Shop = Shop || {};
  3. Then create the Models namespace. It will be an object or the value it has before, in case it exists:

    Shop.Models = Shop.Models || {};
  4. Define the product model with its dependencies. Remember, the first value is the product itself. This allows us to extend the model in case we use many files to define it. So, we define the product model as follows:

    Shop.Models.Product = (function(){
  5. Pass the dependencies. This time you just need to use the Knockout dependency to use observables. Knockout is a global object and there is no need to add it to the dependencies, but it is a good practice to do it as in the following code:

    Shop.Models.Product = (function (ko){
  6. Finally, set the code we had in the...