How to I replace an entire object(created using mapping plugin) inside the viewmodel with a new one(created using the mapping plugin)? Please note my comment inside the view model...
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']);
});
The below function is only called once. From the next time onwards, I need to just replace self.profile whenever I retrieve new data from the server. If I use self.profile=ko.observable(objServerModel), it doesn't work. How do I replace the profile object inside the viewmodel whenever I load new data from the server asynchronously?
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;
}
I would appreciate any help.
Thanks
Edit: The replaced mapped models structure will be the same. Only values will differ.
Here is my jsfiddle : http://jsfiddle.net/rzFgU/