我創建這裏的問題的一個例子沒有更新表:KnockoutJS - 當陣列更新
http://jsfiddle.net/JustinN/qWeLT/1/
我實際的代碼是掛接到ASP.NET Web方法,因此示例代碼已經調整指向一個公共Web服務來演示。
基本上,我有一個數組,我綁定,但是在ajax調用後,表不顯示更新的信息。我不確定我錯過了什麼,我首先想到了我需要的地圖插件,所以我試過了,但我還沒有到任何地方。
當然,我不打算在每次數據更改時應用綁定。
[編輯]
的JavaScript下面:下面
var ViewModel = function() {
var self = this;
self.items = ko.observableArray([]);
self.refresh = function() {
$.ajax({
type: "POST",
url: "http://api.wipmania.com/json",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
ko.mapping.fromJS(data.d, self.items.address);
}
});
};
}
var viewModel = new ViewModel();
$(function() {
ko.applyBindings(viewModel);
viewModel.refresh();
});
HTML:
<table data-bind="visible:items.length > 0">
<thead>
<tr>
<th>Country</th>
<th>Code</th>
</tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text:country"></td>
<td data-bind="text:country_code"></td>
</tr>
</tbody>
</table>
對不起,我用來粘貼我的代碼,但後來被要求提供JSFiddle的例子。我現在將包括兩個。 - 編輯和修復。 – JustinN 2012-03-29 09:16:49
你在這行裏有'.address'是什麼? 'ko.mapping.fromJS(data.d,self.items.address);' – Niko 2012-03-29 09:29:53
好的交易。是的,複製和粘貼是最好的(令人驚訝的是人們重新輸入的頻率 - 然後引入無關的錯誤!)和現場鏈接作爲*附件*(我更喜歡http://jsbin.com,但這是一個品味問題)非常棒。 – 2012-03-29 09:31:41