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

toJS erroneously un-maps properties manually added to the viewModel

Open cspotcode opened this issue 13 years ago • 7 comments

While implementing a fix for issue #96, I came across something wonky. I'm not sure if it's a bug or intended behavior.

According to the Knockout docs, properties that are manually added to a viewModel after it's been mapped by fromJS will not appear in the output of toJS. toJS only unmaps properties that were either originally mapped by fromJS or are explicitly included.

However, this behavior does not apply to properties that are manually added to child objects of the root viewModel, or children of children, and on and on.

Reproduction: http://jsfiddle.net/cspotcode/Kehar/

If this is, in fact, a genuine bug, I believe the faulty logic is at: https://github.com/SteveSanderson/knockout.mapping/blob/cf9b978a79dd1f14d53cd0124f32ea334e5ca3b0/knockout.mapping.js#L697

It's looking for __ko_mapping__ on unwrappedRootObject. When ko.mapping.visitModel recurses, unwrappedRootObject becomes a child of the original root object and thus doesn't contain the __ko_mapping__ object.

cspotcode avatar Aug 03 '12 03:08 cspotcode

Yep, that's a bug as well. As long as you're in a fixing mood... :) Seriously though, thanks for reporting this. If you can easily fix it, please go ahead, otherwise I'll take a look asap.

sagacity avatar Aug 03 '12 07:08 sagacity

I might be able to fix it this weekend while working on the other stuff. If not I'll let you know.

cspotcode avatar Aug 03 '12 16:08 cspotcode

Any news if there will be a bugfix available? I think i hit the same problem, having an existing model which was serialized with toJSON to the DB, loaded back from the DB but the model was extended with some new observables.

Right now i help myself with adding all new observables of the model to the include property, but i guess this is not the way it should be handled.

mgraupner avatar Sep 12 '12 10:09 mgraupner

@cspotcode how is this going?

sagacity avatar Oct 21 '12 21:10 sagacity

Apologies, life's been crazy lately. I'll try to carve out some time later this week to work on a fix.

cspotcode avatar Oct 23 '12 14:10 cspotcode

I ran across this now and found the behavior surprising. It wasn't my expectation that calling toJS would only include fields that had been previously mapped using fromJS. In my use case, I need to be able to add properties and have them included in the serialization.

It looks like I can work around this by wiping out the ko_mapping field manually, even though it feels kind of dirty. I could use include, but I don't want to keep track of all the properties that might get added.

scriby avatar Jan 28 '13 01:01 scriby

Hi all :) Meet yet another person interested in this bug to be fixed! Currently I have to mannually monitor&exclude properties before sending it back. It's awful!

gerich-home avatar Feb 14 '14 10:02 gerich-home