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

Deleting a product


To delete a product, follow some simple steps as you did with the CREATE and UPDATE methods.

  1. The first step is to create the mocks in the mocks/products.js file, as follows:

    $.mockjax({
      url: /^\/products\/([\d]+)$/,
      type:'DELETE',
      dataType: 'json',
      responseTime: 750,
      status:200,
      responseText: {
        'data': {
          text: 'Product deleted'
        }
      }
    });
  2. This method is quite easy. Just add a button like the edit button and then the action to remove it.

    var deleteProduct = function (product){
      ProductResource.remove(product.id())
      .done(function(response){
        catalog.remove(product);
        filteredCatalog(catalog());
        removeFromCartByProduct(product);
      });
    };
  3. Create a function to remove the product from the cart. This function iterates over the cart items and locates the cart item which is related to the removed product. Once this item is located, you can remove it as a normal item using the removeFromCart function:

    var removeFromCartByProduct = function (product...