knockout.viewmodel
knockout.viewmodel copied to clipboard
TypeError: g.pushFromModel is not a function
I have an issue in which I get the message "TypeError: g.pushFromModel is not a function". This happens when I call ko.viewmodel.updateFromModel for the second time. The code is as follows:
viewModel = {
lists: null
};
var koViewModel = ko.viewmodel.fromModel(viewModel, {
extend: {
"{root}": function (model) {
model.selectedList = ko.observable(null);
model.isOnLine = ko.observable(false);
model.selectList = function (list) {
model.selectedList(list);
};
model.addList = function (list) {
model.lists.push(list);
};
model.removeList = function (list) {
model.lists.remove(list);
};
}
});
// Mind that viewModel.lists = null which is correct, no data here.
ko.applyBindings(koViewModel );
// Load data from the cache, this goes well. Data is presented on the screen.
viewModel.lists = JSON.parse(jsonCache);
ko.viewmodel.updateFromModel(koViewModel, viewModel);
// Load data from the server and update "cached" data
viewModel.lists = [data from server, which is correct (identical to cached)];
ko.viewmodel.updateFromModel(koViewModel, viewModel);
The last line gives me the error "TypeError: g.pushFromModel is not a function". In fact, when I call the function ko.viewmodel.updateFromModel(koViewModel, viewModel); twice in the first section, I get the message as well.
Have you found a solution for this? I'm hitting exactly the same issue.
I have same problem, any solution?
same here, anyone know if this project is still being actively maintained?
It seems if the data being updated does not exactly match the structure you have, for example if you try to update an observablearray with a string, that causes this error.
In my case data coming from server was a string and I forgot to turn it into JSON first like:
var dataFromServer = ko.utils.parseJson(JSONdataFromServer);
before
ko.viewmodel.updateFromModel(koViewModel, viewModel);
After I added that error went away and it worked. Hope that helps others with this issue.