Skip to content

Data: align data accessors with the ultimate "Embrace HTML5" direction #2257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timmywil opened this issue May 4, 2015 · 3 comments
Closed

Comments

@timmywil
Copy link
Member

timmywil commented May 4, 2015

camelCasing in .data() has always been problematic. Our attempts to work around camelCasing whenever possible has always caused problems. We've decided to just always camelCase. Not only will our implementation align with the dataset spec, but it makes everything a lot simpler.

I've made a separate issue because #1751 is not broad enough in scope. It only pertains to keys with dashes and digits, which will also be fixed, but in a separate commit.

The solution as @gibson042 laid out in this comment, is this...

assert.equal( jQuery("<div>").data({ "a-b": 1 }).data( "a-b", 2 ).data( "a-b" ), 2 )

And per #1515 (comment) , we want the "Embrace HTML5" option:

  • camelCase data-* attributes into the data object on first access
  • set only camelCase key on .data( key, val ) (setter)
  • camelCase all the added keys on .data( obj ) (bulk setter)
  • look for only camelCase key on .data( key ) (getter)
  • ignore direct manipulation from operations on .data()
@rwaldron
Copy link
Member

rwaldron commented May 4, 2015

I thought I had posted a question when I was mobile this morning, but I guess it didn't go through...

There is no mention (maybe there is, and I missed it?) of this use case:

jQuery("<div>").data({ "a-b": 1 }).data(); // => { "a-b": 1}

Which is definitely broken now, but if that was intentional (which is fine, I guess), then it needs to be made really clear in the docs. Undoubtedly, there is code that expects to be able to serialize and deserialize that data without jQuery screwing with it.

@timmywil
Copy link
Member Author

timmywil commented May 4, 2015

jQuery("<div>").data({ "a-b": 1 }).data();

Right, we have intentionally broken this case. We will be covering this in the migrate plugin and docs.

@timmywil
Copy link
Member Author

timmywil commented May 4, 2015

That said, jQuery("<div>").data({ "a-b": 1 }).data("a-b") === 1 still works.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants