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

Incorrect handling of properties with periods (e.g. "foo.bar")

Open cspotcode opened this issue 13 years ago • 10 comments

knockout.mapping's internal design assumes that properties will not have periods, but this is never mentioned in the documentation.

I made a JSFiddle demonstrating the problem: http://jsfiddle.net/cspotcode/JtkGC/6/

In the fiddle, plainJS["a.b"] is treated the same way as plainJS["a"]["b"] (it's ignored)

I'm not sure what the proper solution should be.

cspotcode avatar Aug 02 '12 02:08 cspotcode

I don't think this behavior can be changed without breaking backwards compatibility. In your case for instance, there is no way for the plugin to figure out if you wanted to ignore ["a.b"] or ["a"]["b"]. I'm also not sure how to best solve this (while maintaining compatibility).

sagacity avatar Aug 02 '12 07:08 sagacity

Current behavior hides both ["a.b"] and ["a"]["b"]. However, I'm guessing most people think that correct behavior is to hide only ["a"]["b"]. Thus hiding ["a.b"] is a bug which can be fixed without breaking backwards compatibility.

If that's the case, we can change current behavior to escape periods within property names. So if I wanted to hide ["a.b"] I'd have to do: var mappingOptions = { ignore: ["a\\.b"] };

This allows crazy people with periods in their property names to construct non-ambiguous mapping options, and it doesn't break compatibility for everyone else.

cspotcode avatar Aug 02 '12 16:08 cspotcode

The escape characters are certainly a fair suggestion. I wouldn't suggest that people using that syntax are crazy per se, though. :)

Would you like to take a stab at fixing it? The code you should be looking at is probably the fullPropertyName-related code.

sagacity avatar Aug 02 '12 21:08 sagacity

I'd be delighted (time permitting) I'll try to throw something together tonight.

Agreed on not calling people crazy, especially since I submitted this bug report :D

cspotcode avatar Aug 02 '12 21:08 cspotcode

:) Okay, good! If you have any questions, just add them to this issue. It might be a good idea to just take your fiddle-code and use that as a failing integration test to start with, and build from there.

sagacity avatar Aug 02 '12 21:08 sagacity

Just a heads-up, I'm still keen on fixing this problem but first I have to get Knockout added to my employer's "we don't own your inventions on these projects" list. ("Intellectual Property Disclosure Form"? I forget what it's called...)

cspotcode avatar Aug 14 '12 18:08 cspotcode

Okay, that certainly seems like something you'd want to have filled out!

sagacity avatar Aug 15 '12 07:08 sagacity

@cspotcode have you made any progress on filling out that form? :)

sagacity avatar Oct 21 '12 21:10 sagacity

Yeah, sorry for the extended radio silence. I talked to my boss and verified that this kind of contribution (bugfixing) is allowed.

I'll try to carve out some time later this week to work on a fix.

cspotcode avatar Oct 23 '12 14:10 cspotcode

@cspotcode No worries, just checking if you were still with us :)

sagacity avatar Oct 23 '12 20:10 sagacity