var repo = {
desc: 'this is repo desc.',
title: 'this is title.',
job: { jobId: 1, description: 'job desc', status: 'Done', workAllowed: 'Yes' },
fault: { faultId: 1, jobId: 1, faultDescription: 'some thing', removed: 1, },
};
var mappingSettings = {
'job': {
create: function (options) {
options.data.status = "this has been created.";
return options.data;
},
update: function(options) {
options.data.status = "this has been updated";
return options.data;
}
}
};
$(document).ready(function () {
viewModel = ko.mapping.fromJS(repo, mappingSettings);
ko.applyBindings(viewModel);
});
当我使用mappingSetting将repo映射到viewModel时,我找到了viewModel.status:“这已经更新了”
答案 0 :(得分:1)
创建是第一次创建模型时,如果再次映射则更新触发器。
viewModel = ko.mapping.fromJS(repo, mappingSettings);
//Update again
ko.mapping.fromJS(repo, mappingSettings, viewModel);