-
Notifications
You must be signed in to change notification settings - Fork 20.6k
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
[1.9 vote] jQuery.fn.data( callback( index, data ) ) - Fixes #12397 #910
Conversation
+1, Nice consistency with the rest of proto method APIs What about something like... $( selector ).data(function( k , data ) {
return {
a: data.a || "alpha",
b: "beta"
};
}); |
The $( selector ).data(function( k , data ) {
jQuery.extend( data, {
a: data.a || "alpha",
b: "beta"
});
}); I think making the backend of the API do this would just pollute it, would people would also expect missing keys to be deleted? |
Dealing with two conflicting patterns here, the It might make sense to pass the return value into |
@rwldrn - If we wanted to try to be consistent with the other collection setters it might look something like this: if ( jQuery.isFunction( key ) ) {
return this.each( function( index ) {
var result = key( index, jQuery( this ).data() );
if ( typeof result === "object" ) {
jQuery( this ).data( result );
}
});
} 10 bytes heavier on the gzip size - perhaps useful? |
Sorry, I was both +1'ing the addition for being consistent and separately asking about API possibilities. I was and still am mobile, so I hadn't actually tested that, just tapped it out ;) |
@gnarf37 Using the returned value to update data seems consistent with other APIs, but the |
// Calls callback with index, data | ||
if ( jQuery.isFunction( key ) ) { | ||
return this.each( function( index ) { | ||
key.call( this, index, jQuery( this ).data() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better use $.data() instead, for performance reasons...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency we can't. It must pass through the chunk of code that can read html attrs.
@rkatic If a string, boolean, etc is returned, it can't be set to "data" -- only objects can be extended onto the data object. |
@gnarf37 Right... for a moment I supposed it was about Btw. also |
Not voted in, see ticket. |
Proposal for a new signature to
jQuery.fn.data()
Trac Ticket: http://jqbug.com/12397