On Page load I am converting a JSON data to view model like this
var jsonField = '#' + '<% = hdnField.ClientID %>';
jsonModel= {
availables: ko.observableArray([])
};
var valueField = $(jsonField)[0].value;
var arrayGroup = $.parseJSON(valueField);
jsonModel.availables = ko.viewmodel.fromModel(arrayGroup);
var block=$('#availabilitiesBlock')[0];
ko.applyBindings(jsonModel,block);
After the page has loaded, I am calling an ajax server function to update this view model.
In tha ajax success call back I have written like
var updatedModel = {
availables: ko.observableArray([])
};
updatedModel.availables = ko.viewmodel.fromModel(data.d);
When I try to update the view model with this updated model my entire model becomes blank
I tried the following methods
Pushed data.d into the oldModel.availables observable. When i push the data, the aray objects are not getting converted as observables, but the items got added to the availables array. Whereas in the initial (while page load), the objects got converted without issues.
Tried to update viewmodels directly and in this case entire model becomes blank
ko.viewmodel.updateFromModel(oldModel, updatedModel);
Snip of Old Model
Snip of Updated Model
As I said , when I pushed the array objects directly (data.d), the observables are not getting created automatically as in the first case before ajax.
Why is this happening when the logic is same? I am using http://coderenaissance.github.io/knockout.viewmodel/ for mapping objects automatically.
Any pointers will be helpful