如何使用新插件(使用映射插件創建)替換視圖模型內的整個對象(使用映射插件創建)?請注意我在視圖模型中的評論...替換使用新映射插件創建的挖空視圖模型
function viewmodel(objServerModel) {
var self = this;
self.profile = objServerModel; //Using ko.observable(objServerModel) doesn't seem to work. I need to replace this object every time I load new content from the server and it should automatically update all controls bound to it. Note that this is the object that is created using ko.mapping.fromJS
self.devices = ko.observableArray(['one', 'two']);
});
下面的函數只調用一次。從下一次開始,當我從服務器獲取新數據時,我只需要替換self.profile。 如果我使用self.profile = ko.observable(objServerModel),它不起作用。每當我從服務器異步加載新數據時,如何替換viewmodel中的配置文件對象?
function ApplyKnockOut() {
var objServerModel = '@Html.Raw(HttpUtility.JavaScriptStringEncode(clsTest.ReadXmlIntoModel()))'
var objResult = $.parseJSON(objServerModel);
var objModelFromServer = ko.mapping.fromJS(objResult);
var clientViewModel = new viewmodel(objModelFromServer);
ko.applyBindings(clientViewModel);
return clientViewModel;
}
我將不勝感激任何幫助。
謝謝
編輯:被替換的映射模型結構將是相同的。只有值會有所不同。
這裏是我的jsfiddle:http://jsfiddle.net/rzFgU/
這是通過設計,它只更新已更改的字段,以便只更新綁定。 – Anders