我在设置敲除数据绑定到动态生成的输入字段时遇到问题。快速浏览canActivate(id),我可以获得页面上所需的所有对象的详细信息,并将其存储在vm.OPCLayoutVm中。
var title = 'User Information';
var vm = {
canActivate: canActivate,
attached: attached,
title: title,
OPCLayoutVm: null,
UserInformationVm: ko.observable({}),
UserId:null
};
return vm;
function canActivate(userId) {
var dfd = $.Deferred();
vm.userId = userId.patientId;
if (userID != null) {
$.getJSON('CoreData/GetCoreData').done(function(data) {
vm.OPCLayoutVm = buildViewModel(data);
return dfd.resolve(true);
}).fail(function(e) {
logger.logError('Couldn\'t load core layout', e, 'user-information', true);
return dfd.resolve(false);
});
} else {
return dfd.resolve(true);
}
return dfd;
}
function buildViewModel(data) {
var data_response = data.result;
return data_response;
}
然后,一旦页面加载,我填充动态字段,如果需要额外数据,请发送JSON请求以获取它,然后将其存储为vm.UserInformationVm中的新对象
function attached() {
fnCreateInformationPageStructure();
}
function fnCreateInformationPageStructure() {
//Send json request to get all objects
// go through all objects and create them
// however on specific objects where data is already present send off another json request to get it...
// add binding details to dynamic object
$(inputDiv).find('input')[0].setAttribute("data-bind", "value: vm.UserInformationVm." + (matching data name to the json returned data below) + ".data");
//request data and add to vm.UserInformationVm
if (dataItem.DatabaseTableName != null && dataItem.TableFieldName != null) {
$.getJSON("CoreData/GetObjectData?id=" + vm.UserId + '&tableName=' + dataItem.DatabaseTableName + '&columnName=' + dataItem.TableFieldName).done(function (result) {
extendObject(vm.UserInformationVm, [result.result[0].column, new DataBindObject(result.result[0].data,result.result[0].columnName,result.result[0].tableName)]);
});
}
}
// Other used function
function extendObject(obj, props) {
for (var i = 0; i < props.length; i+=2)
obj[props[i]] = props[i+1];
}
function DataBindObject(dataitem, columnName, tableName) {
this.data = ko.observable(dataitem);
this.column = columnName;
this.table = tableName;
}
然而,正如我所期望的那样运行所有内容,带有data-bind的输入字段只是不想在vm.UserInformationVm。(相关对象)中显示数据。
有什么想法吗?
-Update-
UserInformationvm的当前数据结构看起来像......
-UserInformationVm
-userForename (dynamically generated)
-userSurname (dynamically generated)
-data (ko.observable)
-table
-column
答案 0 :(得分:0)
修改强>
你的PatientInformationVm应该是一个可观察对象,不应该吗?
然后,您将使用
替换您的extendObject函数function extendObject(obj, props) {
for (var i = 0; i < props.length; i+=2)
if (ko.isObservable(obj[props[i]]))
obj[props[i]](props[i+1]);
else
obj[props[i]] = props[i+1];
if (ko.isObservable(obj)) obj.valueHasMutated();
}