…nd Paul Irish. Fixes #6921.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
(function( jQuery ) { | ||
|
||
var windowData = {}; | ||
var windowData = {}, | ||
rnum = /^-?[0-9.]$/; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
jQuery.extend({ | ||
cache: {}, | ||
@@ -142,8 +143,23 @@ jQuery.fn.extend({ | ||
if ( value === undefined ) { | ||
var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]); | ||
|
||
// Try to fetch any internally stored data first | ||
if ( data === undefined && this.length ) { | ||
data = jQuery.data( this[0], key ); | ||
|
||
// If nothing was found internally, try to fetch any | ||
// data from the HTML5 data-* attribute | ||
if ( data === undefined && this[0].nodeType === 1 ) { | ||
data = this[0].getAttribute( "data-" + key ); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jeresig
Author
Member
|
||
|
||
if ( data != null ) { | ||
data = data === "true" ? true : | ||
data === "false" ? false : | ||
data === "null" ? null : | ||
rnum.test( data ) ? parseFloat( data ) : | ||
This comment has been minimized.
Sorry, something went wrong.
jakearchibald
|
||
data; | ||
} | ||
} | ||
} | ||
|
||
return data === undefined && parts[1] ? | ||
18 comments
on commit 20673d7
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.
w00t!
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.
Nice!
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.
great update! thanks..
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.
Shouldn't /^-?[0-9.]$/
be /^-?[0-9\.]+$/
?
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.
@jason - Derp, yep - forgot the + - but the escaping \ isn't needed.
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.
totally makes sense! thanks for the addition!
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.
No parsing of JSON into an object?
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.
I basically understand how this is working, and it looks great! However, there is one thing I'm unclear on. The regular expression "rnum" seems to only match strings that start with numbers between -9 and 9 inclusive, "." and "-.". Why is that type of string important to differentiate from other strings for this functionality?
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.
@jfirebaugh: I don't think jQuery should have to guess what type of data is stored in the attribute as a string?
@jeremyckahn: It determines if the attribute looks like a number and converts it accordingly. E.g. "-1.27" → -1.27
.
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.
@gf3: Thank you for clearing that up, that makes sense. However, isn't that potentially dangerous? It seems that the string "-1.27_not_a_number" would be parsed as a float.
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.
@jfirebaugh: That was certainly an option - but the performance overhead of implementing that would've been excessive. It would've resulted in a complete parse of every found attribute - there's no way that it would've been fast enough. Instead I opted to cover some of the common cases (true, false, null, and numbers).
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.
Detection of JSON was part of the original proposal and patch by Paul Irish, and is necessary if this is intended to be a full replacement for the metadata plugin.
The cool thing about how the metadata plugin works is how it integrates with jQuery UI widgets. For example, if I'm using the jQuery button widget and I want to give my buttons different icons, I can do the following:
<button data-button='{"icons": {"primary": "ui-icon-plus"}}'>New</button>
<button data-button='{"icons": {"primary": "ui-icon-trash"}}'>Delete</button>
$('button').button();
And both buttons automatically get the right icon. In other words, I can supply all the widget options in the data attribute, serialized as JSON, and make only a single call to instantiate everything.
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.
@jeresig: Could we try the JSON parse only if the value starts with '{' and ends with '}'?
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.
@jfirebaugh: In other other words you can supply presentation data in functionality data in information data. Hmmm....
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.
FYI for those following this -- the original discussion is here: http://forum.jquery.com/topic/patch-feedback-requested-html5-data-attrs-transcend-into-the-fn-data-api
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.
@jeremyckahn: The RegExp isn't that permissive, it'll reject strings like "1.27foo" because it has a non-number in it. The RegExp isn't perfect though - it'll try to handle things like "1..2" and "-." so I may want to tweak it some.
@jfirebaugh: That might be a possibility, I can check in to it. As it was originally implemented by Paul + co., though, it was blindly running parseJSON on every attribute, which just wasn't good.
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.
@jeresig: That makes sense. Thanks for the explanation!
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.
@jfirebaugh, @jeresig: Proper JSON can be an array too. I.e. starting with '[' and ending with ']', so rbrace should be adjusted accordingly: rbrace = /^(?:\{.*\}|\[.*\])$/;
Maybe translate the JSON grammar for number to tighten this up?