knockout.viewmodel icon indicating copy to clipboard operation
knockout.viewmodel copied to clipboard

TypeError: g.pushFromModel is not a function

Open pietvis opened this issue 11 years ago • 4 comments

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.

pietvis avatar Jun 10 '14 20:06 pietvis

Have you found a solution for this? I'm hitting exactly the same issue.

Devqon avatar May 08 '15 09:05 Devqon

I have same problem, any solution?

DjCzermino avatar May 28 '15 07:05 DjCzermino

same here, anyone know if this project is still being actively maintained?

enispilavdzic avatar Jun 17 '16 22:06 enispilavdzic

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.

enispilavdzic avatar Jun 17 '16 23:06 enispilavdzic