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
chap2
jason.zhu 2021-05-27 02:46:22 +00:00
parent 4e774c2585
commit d7d701f343
3 changed files with 20 additions and 8 deletions

View File

@ -30,6 +30,7 @@
<script type="text/javascript" src="js/bootstrap.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/knockout-3.2.0.js"></script> <script type="text/javascript" src="js/knockout-3.2.0.js"></script>
<!-- app --> <!-- app -->
<script type="text/javascript" src="js/models/product.js"></script>
<script type="text/javascript" src="js/viewmodel.js"></script> <script type="text/javascript" src="js/viewmodel.js"></script>
</body> </body>
</html> </html>

View File

@ -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,
};
};

View File

@ -1,9 +1,7 @@
var vm = { var vm = (function() {
product: { product: Product(1, 'T-Shirt', 10, 20);
id: 1, return {
name: 'T-Shirt', product: product
price: 10,
stock: 20
}
}; };
})();
ko.applyBindings(vm); ko.applyBindings(vm);