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

Setting CRUD operations in the resource


We are going to define some services to define CRUD operations for our products and orders. A common mistake that some developers make is setting CRUD operations within model classes. Best practice says that it is better to separate models and communication layers.

To prepare your project, create a folder called services. In this folder, store files that will contain CRUD operations. Perform the following steps:

  1. Create two files in the new folder. They represent two communication services: OrderResource.js and ProductResource.js.

  2. Open the ProductResource.js file and define basic CRUD operations as follows:

    var ProductResource = (function () {
      function all() {}
      function get(id) {}
      function create(product) {}
      function update(product) {}
      function remove(id) {}
      return {
        all: all,
        get: get,
        create: create,
        update: update,
        remove: remove
      };
    })();

    This is the skeleton of the CRUD service. You use the all and get methods to define...