Book Image

Knockout.JS Essentials

Book Image

Knockout.JS Essentials

Overview of this book

Table of Contents (16 chapters)
KnockoutJS Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

The Resources module


In terms of code, building a module that contains a model and building one that contains a resource is not so different. The module pattern applied is the same. Nevertheless, you don't need to create instances of the resources. To apply CRUD operations to the models, you just need an object that handles this responsibility. Therefore, the resources will be singletons, as done in the following steps:

  1. Open the resources/ProductResource.js file.

  2. Create the top hierarchy module:

    var Shop;
    Shop = Shop || {};
  3. Create the Resources namespace:

    Shop.Resources = Shop.Resources || {};
  4. Define ProductResource using the module pattern:

    Shop.Resources.ProductResource = (function(){
    })()
  5. Set the dependencies. In this case, jQuery is the dependency you need. jQuery is a global object and you don't need to pass it as a dependency, but it's a good practice to do so.

    Shop.Resources.ProductResource = (function($){
    }(jQuery);
  6. Finally, set the following code in the resources/ProductResource.js file....