From d7d701f343ea10794377b117d171f6a04b4d392f Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Thu, 27 May 2021 02:46:22 +0000 Subject: [PATCH] chap1->The view-model->The model 1. Create separate model 'Product' 2. Place link to product.js in HTML at correct place 3. Learn & apply Revealing Module Pattern on both Product module and viewmodel --- ko-cart/index.html | 1 + ko-cart/js/models/product.js | 13 +++++++++++++ ko-cart/js/viewmodel.js | 14 ++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 ko-cart/js/models/product.js diff --git a/ko-cart/index.html b/ko-cart/index.html index 8bd5644..25684d2 100644 --- a/ko-cart/index.html +++ b/ko-cart/index.html @@ -30,6 +30,7 @@ + diff --git a/ko-cart/js/models/product.js b/ko-cart/js/models/product.js new file mode 100644 index 0000000..d958856 --- /dev/null +++ b/ko-cart/js/models/product.js @@ -0,0 +1,13 @@ +var Product = function (id, name, price, stock) { + "use strict"; + var _id = id, + _name = name, + _price = price, + _stock = stock; + return { + id: _id, + name: _name, + price: _price, + stock: _stock, + }; +}; diff --git a/ko-cart/js/viewmodel.js b/ko-cart/js/viewmodel.js index 63f9805..7e001d3 100644 --- a/ko-cart/js/viewmodel.js +++ b/ko-cart/js/viewmodel.js @@ -1,9 +1,7 @@ -var vm = { - product: { - id: 1, - name: 'T-Shirt', - price: 10, - stock: 20 - } -}; +var vm = (function() { + product: Product(1, 'T-Shirt', 10, 20); + return { + product: product + }; +})(); ko.applyBindings(vm); \ No newline at end of file