KnockOutJs 对象不更新 UI
我正在尝试使用 Ajax 调用更新 KnockOutJS 页面。 Update 函数应从 Web 服务检索 Gifts Observable 数组。这不应该更新模型,然后更新用户界面吗?我一定想念这是如何工作的。我的想法?
谢谢
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<tit
le>Index</title>
<link rel="Stylesheet" href="/Content/Site.css" />
<script type="text/javascript" src="/Scripts/jquery-1.4.2.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script>
<script type="text/javascript" src="/Scripts/knockout-1.2.1.js"></script>
<script type="text/javascript" src="/Scripts/knockout.mapping.js"></script>
<script type="text/javascript" src="/Scripts/json2.js"></script>
<script type="text/javascript" src="http://knockoutjs.com/examples/resources/sampleProductCategories.js">
</script>
</head>
<body>
<div>
<h1>Gift list editor 2</h1>
11/15/2011 12:55:32 PM <br />
<p>You have asked for <span data-bind="text: gifts().length"> </span> gift(s)</p>
<form class="giftListEditor" action="" >
<table style="width: 800px;">
<tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody>
</table>
<button data-bind="click: addGift">Add Gift</button>
<button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
<button data-bind="click: update, enable: gifts().length > 0">Partial Update</button>
</form>
<script type="text/html" id="giftRowTemplate">
<tr>
<td style="width:100px;" >
<label data-bind="visible: GiftId == 0">New</label>
<label data-bind="text: 'GiftId: ' + GiftId, visible: GiftId > 0" />
</td>
<td><select data-bind='options: sampleProductCategories, optionsText: "name"' /></td>
<td>Gift name: <input class="required" data-bind="value: Title, uniqueName: true"/></td>
<td>Price: $ <input class="required number" data-bind="value: Price, uniqueName: true" /></td>
<td><a href="#" data-bind="click: function() { viewModel.removeGift($data) }">Delete</a></td>
</tr>
</script>
<script type="text/javascript">
var initialData = [{"GiftId":13,"Title":"test","Price":3},{"GiftId":15,"Title":"asdasd","Price":444},{"GiftId":44,"Title":"asd","Price":1},{"GiftId":45,"Title":"asdasd","Price":1231},{"GiftId":46,"Title":"qwe","Price":132},{"GiftId":47,"Title":"eeee","Price":123123},{"GiftId":48,"Title":"www","Price":121212}]; //var initialData = [{ GiftId: "","Title": "Tall Hat", "Price": 49.95 }, { GiftId: "","Title": "Long Cloak", "Price": 78.25}];
var viewModel = {
gifts: ko.observableArray(initialData),
addGift: function () {
this.gifts.push({ GiftId: "0", Title: "", Price: "" });
},
removeGift: function (gift) {
this.gifts.remove(gift);
},
update: function () {
$.ajax({
url: "/Home/PartialUpdate",
success: function(result){
this.gifts = ko.utils.parseJson(result); //[{ GiftId: "33", Title: "ThirtyThree", Price: "10.50" }]
alert(this.gifts.length);
},
error:function(xhr,err){
alert("readyState: " + xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: " + xhr.responseText);
}
});
},
save: function() {
ko.utils.postJson(location.href, { gifts: this.gifts });
}
};
ko.applyBindings(viewModel, document.body);
$("form").validate({ submitHandler: function() { viewModel.save() } });
</script>
</div>
</body>
</html>
I am trying to update a KnockOutJS page using an Ajax call. The Update function should retrieve the Gifts Observable array from a web service. Shouldn't this update the model, then the UI. I must be missing how this works. I ideas?
Thanks
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<tit
le>Index</title>
<link rel="Stylesheet" href="/Content/Site.css" />
<script type="text/javascript" src="/Scripts/jquery-1.4.2.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script>
<script type="text/javascript" src="/Scripts/knockout-1.2.1.js"></script>
<script type="text/javascript" src="/Scripts/knockout.mapping.js"></script>
<script type="text/javascript" src="/Scripts/json2.js"></script>
<script type="text/javascript" src="http://knockoutjs.com/examples/resources/sampleProductCategories.js">
</script>
</head>
<body>
<div>
<h1>Gift list editor 2</h1>
11/15/2011 12:55:32 PM <br />
<p>You have asked for <span data-bind="text: gifts().length"> </span> gift(s)</p>
<form class="giftListEditor" action="" >
<table style="width: 800px;">
<tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody>
</table>
<button data-bind="click: addGift">Add Gift</button>
<button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
<button data-bind="click: update, enable: gifts().length > 0">Partial Update</button>
</form>
<script type="text/html" id="giftRowTemplate">
<tr>
<td style="width:100px;" >
<label data-bind="visible: GiftId == 0">New</label>
<label data-bind="text: 'GiftId: ' + GiftId, visible: GiftId > 0" />
</td>
<td><select data-bind='options: sampleProductCategories, optionsText: "name"' /></td>
<td>Gift name: <input class="required" data-bind="value: Title, uniqueName: true"/></td>
<td>Price: $ <input class="required number" data-bind="value: Price, uniqueName: true" /></td>
<td><a href="#" data-bind="click: function() { viewModel.removeGift($data) }">Delete</a></td>
</tr>
</script>
<script type="text/javascript">
var initialData = [{"GiftId":13,"Title":"test","Price":3},{"GiftId":15,"Title":"asdasd","Price":444},{"GiftId":44,"Title":"asd","Price":1},{"GiftId":45,"Title":"asdasd","Price":1231},{"GiftId":46,"Title":"qwe","Price":132},{"GiftId":47,"Title":"eeee","Price":123123},{"GiftId":48,"Title":"www","Price":121212}]; //var initialData = [{ GiftId: "","Title": "Tall Hat", "Price": 49.95 }, { GiftId: "","Title": "Long Cloak", "Price": 78.25}];
var viewModel = {
gifts: ko.observableArray(initialData),
addGift: function () {
this.gifts.push({ GiftId: "0", Title: "", Price: "" });
},
removeGift: function (gift) {
this.gifts.remove(gift);
},
update: function () {
$.ajax({
url: "/Home/PartialUpdate",
success: function(result){
this.gifts = ko.utils.parseJson(result); //[{ GiftId: "33", Title: "ThirtyThree", Price: "10.50" }]
alert(this.gifts.length);
},
error:function(xhr,err){
alert("readyState: " + xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: " + xhr.responseText);
}
});
},
save: function() {
ko.utils.postJson(location.href, { gifts: this.gifts });
}
};
ko.applyBindings(viewModel, document.body);
$("form").validate({ submitHandler: function() { viewModel.save() } });
</script>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
this.gifts
是一个 observableArray。当您设置更新的值时,您需要将其设置为可观察的:您当前的代码正在将gift设置为等于不可观察或当前绑定到UI的新数组。
您可能还需要小心成功函数中的
this
。如果this
不合适,那么您可以选择将context: viewModel
添加到 $.ajax 选项中。this.gifts
is an observableArray. When you set the updated value, you need to set it as an observable:The current code that you have is setting gifts equal to a new array that is not observable or currently bound against the UI.
You may also need to be careful with
this
in your success function. Ifthis
is not appropriate, then you could choose to add acontext: viewModel
to your $.ajax options.