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

The key() function can get called on create()d as well as un-create()d items

Open rix0rrr opened this issue 12 years ago • 2 comments
trafficstars

This leads to a problem when the create() function does a more complex transformation than adding a prototype to a bunch of JSON data, or adding computed observables.

Specifically, it leads to trouble in a use-case where the incoming values are IDs, which get transformed to complex objects by the create() function.

In that case, the key() function both gets called on the input data (integers) and the values in the mapped collection (complex objects), which can't be desirable behaviour.

Example fiddle: http://jsfiddle.net/hRMux/3/

(Also, I've noticed that in the remove() operation, key() gets called multiple times on the value to-be-removed, once for every comparison, which is superfluous)

rix0rrr avatar Nov 23 '12 11:11 rix0rrr

Yes, you're right. The problem of having a large API surface is that stuff that I don't personally use (i.e. mappedRemove) can be a bit untested. I'll need to take a look and see what's causing it.

If you would like to attempt a fix yourself that would be most welcome. As a fellow Eindhoven developer, that's something that would earn you lots of bonuspoints. :)

sagacity avatar Nov 26 '12 13:11 sagacity

I would love to help, but I have to confess I'm not totally sure what the correct behaviour ought to be :). Anyway, I will try to take a crack at it (when I can find the time).

Didn't notice you where from Eindhoven, heh :).

rix0rrr avatar Nov 26 '12 13:11 rix0rrr