coderenaissance / knockout.viewmodel

The knockout viewmodel plugin is the fastest, smallest, cleanest, most flexible way to create a knockout viewmodel.
http://coderenaissance.github.com/knockout.viewmodel
105 stars 27 forks source link

Custom unmapping leads to infinite recursion #40

Open aknuds1 opened 11 years ago

aknuds1 commented 11 years ago

Calling ko.viewmodel.toModel from a custom unmap function, as seen in one example on the project page, leads to infinite recursion. The example from the project page looks as follows:

options:{ 
    custom:{
        "{root}.users[i]": {
            map:function(user){
                var mapped = ko.viewmodel.fromModel(user);
                mapped().isDeleted= ko.observable(false);
                return mapped;
            },
            unmap:function(user){
                var unmapped = ko.viewmodel.toModel(user);
                delete unmapped.isDeleted;
                return unmapped;
            }
        }
    }
};

The call to ko.viewmodel.toModel in unmap leads to unmap being called recursively in my experience (using knockout.viewmodel 2.0.3 and Knockout 2.3.0). See my example fiddle to reproduce, although I've commented out the unmap function initially because it causes the page to crash in Chrome,

coderenaissance commented 11 years ago

I'll take a look at this problem for the release after the current one I'm working on (which I'm trying like crazy to make the time to wrap up). Thanks for your help.

fgieseke commented 9 years ago

Ups, I created a new Issue for this problem. But at least I described a workaround: See https://github.com/coderenaissance/knockout.viewmodel/issues/64