I am pretty new at using KnockoutJS as a framework, and I (obviously) encountered a question I cannot (or more likely, am not skilled enough to) find.
I have this product group and each product group contains array of products. Now, I managed to display a list of product groups and a list of all products without much problem. I want to increase product quantity, when user left-clicks a div containing product and decrease product quantity when user right-clicks a div containing products.
I have set up event handlers correctly as they get fired and handled without any problems and quantity is changed, but I cannot seem to get it to reflect on the page.
Markup:
<div class="viewProductGroup" data-bind="with: productGroupData">
<h2 data-bind="text: Title"></h2>
<div class="stickies" data-bind="foreach: Products">
<div data-bind="click: $root.addProduct,
event: { contextmenu: $root.removeProduct }">
<span data-bind="text: Title"></span><br />(<span data-bind="text: Quantity"></span>)</div>
</div>
</div>
Excerpt from JS:
function StickyExViewModel() {
var self = this;
self.productGroupData = ko.observable();
// Behaviors
self.addProduct = function (item) {
item.Quantity++;
}
self.removeProduct = function (item) {
item.Quantity--;
}
}
ko.applyBindings(new StickyExViewModel());
Now, I am sure I don't understand something or have missed some wiring. Can you please help?
Edit
Here is JSON obtained from WCF service:
{"d":
{"__type":"ProductGroup:#Stickyex.Services",
"Created":"\/Date(1373407200000+0200)\/",
"ID":1,
"Products":[
{"__type":"Product:#Stickyex.Services","Code":"0","ID":0,"Quantity":0,"Title":"0"},
{"__type":"Product:#Stickyex.Services","Code":"1","ID":1,"Quantity":1,"Title":"1"},
{"__type":"Product:#Stickyex.Services","Code":"2","ID":2,"Quantity":2,"Title":"2"}],
"Title":"ProductGroup 1"}
}
Code to obtain JSON data (inside StickyExViewModel):
self.goToProductGroup = function(productGroup) {
$.get(serviceUrl, { ixProductGroup: productGroup.ID }, function (data) {
self.productGroupData(data.d);
});
}