我是新來使用Knockoutjs,我一直有一個問題,我難住。我從服務器獲得一個json對象,這是一個對象集合,我試圖使用knockoutjs將它綁定到一個列表。有一些我雖然失蹤。我不確定如何引用正在綁定的視圖模型中的當前對象。databinding JSON收集與knockoutjs
function GetGameListResponse(response)
{
if (response.Error == null)
{
// this is my test, when I bind the collection directly everything works fine...
//ko.applyBindings(response, document.getElementById('panGames'));
// this doesn't work
ko.applyBindings(new ListViewModel(response), document.getElementById('panGames'));
}
}
function ListViewModel(response)
{
var self = this;
// this is where the problem is I think, as 'response'
self.Id = ko.observable(response.Id);
self.Name = ko.observable(response.Name);
self.Date = ko.observable(response.Date);
self.Description = ko.observable(response.Description);
}
...這裏是將其綁定到HTML:
<table>
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Description</th>
<th>select</th>
</tr>
</thead>
<tbody data-bind="foreach: List">
<tr>
<td data-bind="text: Name"></td>
<td data-bind="text: Date"></td>
<td data-bind="text: Description"></td>
<td></td>
</tr>
</tbody>
</table>
的JSON是編號,姓名,日期等對象的可預見的集合,如果我不工作正常嘗試使用視圖模型將集合綁定到UI。我一定是簡單的東西在這裏...
您可以發佈樣本JSON ? – nemesv
{列表:[{名稱:「FirstThing」,日期:「1/22/13」,說明:「Fooo」},{名稱:「SecondThing」,日期:「1/23/13」,說明:「Fooo 「}]} – MadTigger