jason.zhu 9cb6be067a chap1->Observables to refresh the UI automatically->Inserting elements in collections
1. Learned with binding for context switch

2. Learned textInput for text binding

3. click binding for event driven
2021-05-27 04:22:28 +00:00

37 lines
780 B
JavaScript

"use strict";
var vm = (function () {
var catalog = ko.observableArray([
Product(1, "T-Shirt", 10.0, 20),
Product(2, "Trousers", 20.0, 10),
Product(3, "Shirt", 15.0, 20),
Product(4, "Shorts", 5.0, 10),
]);
var newProduct = Product("", "", "", "");
var clearNewProduct = function () {
newProduct.name("");
newProduct.price("");
newProduct.stock("");
};
var addProduct = function (context) {
var id = new Date().valueOf(); // random id from time
var product = Product(
id,
context.name(),
context.price(),
context.stock()
);
catalog.push(product);
clearNewProduct();
};
return {
catalog: catalog,
newProduct: newProduct,
addProduct: addProduct,
};
})();
ko.applyBindings(vm);