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

Custom unmapping leads to infinite recursion

Open aknuds1 opened this issue 12 years ago • 2 comments

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,

aknuds1 avatar Aug 02 '13 09:08 aknuds1

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.

coderenaissance avatar Aug 21 '13 03:08 coderenaissance

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

fgieseke avatar Dec 29 '14 19:12 fgieseke