我有绑定到我的模型的递归模板。问题是我一次修改一个对象,所以如果我单击下一个对象并尝试加载新对象,则模板不会更新。
我的模板:
<div class="portlet-body" id="details-container" data-bind="template: { name: 'packageTemplate', data: viewModel.packageElements() }"></div>
我的模型:
var viewModel;
function packageElementViewModel(data) {
var self = this;
self.packageElements = ko.observable({ Description: ko.observable(), Children: ko.observableArray() });
}
我的初始化:
$.ajax({
'type': 'POST',
'url': 'http://www.someurl.com/',
'data': { PackageId: 1},
'success': function (data) {
viewModel.packageElements = ko.observable(ko.mapping.fromJS(data));
ko.applyBindings(viewModel);
});
当我选择一个新包时,我调用此代码并更新变量,但 UI 没有更新。:
var id = PackageSummary.packageTable.fnGetData($(this).get(0))['Id'];
$.ajax({
'type': 'POST',
'url': 'http://www.someurl.com',
'data': { PackageId: id },
'success': function (data) {
viewModel.packageElements = ko.observable(ko.mapping.fromJS(data));
},
'error': function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
这是我的数据:
{
"Description":"Top",
"Children":[
{
"Description":"Second-Level 1",
"Children":[
{
"Description":"Third Level 1",
"Children":[]
},
{
"Description":"Third Level 2",
"Children":[]
},
{
"Description":"Third Level 3",
"Children":[]
}
]
},
{
"Description":"Second Level 2":[
{
"Description":"Third Level 1",
"Children":[]
},
{
"Description":"Third Level 2",
"Children":[]
},
{
"Description":"Third Level 3",
"Children":[]
}
]
}
]
}