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

Updating a product


In our catalog, we will want to update the value of our product. To complete this action, follow these steps:

  1. First, to update a product you need to mock the URI that handles the action:

    $.mockjax({
        url: /^\/products\/([\d]+)$/,
        type:'PUT',
        dataType: 'json',
        responseTime: 750,
        status:200,
        responseText: {
            'data': {
                text: 'Product saved'
            }
        }
    });
  2. Add a button in each row in the catalog.html view, in the same cell you have the add-to-cart-button component:

    <button class='btn btn-info' data-bind='click: $parent.openEditModal'>
      <i class='glyphicon glyphicon-pencil'></i>
    </button>
  3. Now, open a modal with the data of this product:

    var openEditModal = function (product) {
      tmpProduct = ProductService.clone(product);
      selectedProduct(product);
      $('#editProductModal').modal('show');
    };
  4. The tmpProduct will contain a copy of the object you are going to edit:

    Var tmpProduct = null;
  5. The selectedProduct will contain...