我非常新的使用KnockoutJS作为一个框架,我(显然)遇到了问题,我不能(或者更可能的,我没有足够的技术来)找到。更新项目阵列
我有该产品组和产品的每个组包含的产品阵列。现在,我设法显示产品组列表和所有产品列表,没有太多问题。当用户用鼠标右键单击包含产品的div并减少产品数量时,我想要增加产品数量。
我已正确设置事件处理程序,因为他们被解雇和处理,没有任何问题和数量改变,但我似乎无法得到它反映在页面上。
标记:
<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>
摘自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());
现在,我相信,我不明白的地方或者已经错过了一些布线。你能帮忙吗?
编辑
在这里从WCF服务获得JSON:
{"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"}
}
代码以获得JSON数据(内部StickyExViewModel):
self.goToProductGroup = function(productGroup) {
$.get(serviceUrl, { ixProductGroup: productGroup.ID }, function (data) {
self.productGroupData(data.d);
});
}
你如何让你的数据产品组?也许你正在以错误的方式访问它们 –
然后你使用什么来创建你的数组? ko.mapping.fromJSON()? –
我使用$ .get jQuery函数从WCF服务获取JSON数据。 –