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

toJS unmaps all properties of sub-objects

Open darkroastjava opened this issue 13 years ago • 2 comments

fromJS keeps a log of all properties that are mapped. toJS only unmaps properties that were tracked. But this seems to hold true only for the first level of properties in a tree of objects.

Example: see also http://jsfiddle.net/U4nDp/

var person = ko.mapping.fromJS({ firstname: "Knock", lastname: "McOut", address: { street: "Some Street", number: "123" } }), personJS;

person.fullName = ko.computed(function() { return person.firstname + ' ' + person.lastname; }); person.address.fullStreet = ko.computed(function() { return person.address.number + ' ' + person.address.street; });

personJS = ko.mapping.toJS(person);

personJS does NOT contain fullName (which is correct), but DOES contain person.address.fullStreet.

I'd expect that fullStreet is not unmapped by toJS either. Stepping through the fromJS implementation I can also watch the individual properties of address being added to the mappedProperties, but they seem to be ignored in the toJS implementation.

darkroastjava avatar Mar 29 '12 11:03 darkroastjava

I found the same issue and I created a fiddle at http://jsfiddle.net/Ncx4w/2/ showing this. I love the plugin but I might have to go manual due to this inconsistency.

BentleyDavis avatar Apr 02 '12 22:04 BentleyDavis

Unit test for it:

http://jsfiddle.net/DzenisevichK/tYuxA/

DzenisevichK avatar Apr 10 '12 11:04 DzenisevichK