Side navigation
#10266 closed bug (wontfix)
Opened September 13, 2011 07:56PM UTC
Closed September 19, 2011 04:07PM UTC
Last modified March 09, 2012 10:14PM UTC
Undefined values passed to $(element).data(key,value) break chaining
Reported by: | jsoverson@gmail.com | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | data | Version: | 1.6.4rc1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Example here:
The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain.
This issue is also what likely triggered ticket but that ticket was closed prematurely due to the inability to reproduce.
Attachments (0)
Change History (7)
Changed September 13, 2011 08:52PM UTC by comment:1
Changed September 13, 2011 09:02PM UTC by comment:2
I think setting a value to undefined is a valid use case, but that might be a different discussion.
I think, regardless, by sending an argument in place of the "value" field, retrieving the previously set value is the least expected result.
I threw in the fix and test case here since I was already working in the code:
Changed September 13, 2011 09:08PM UTC by comment:3
component: | unfiled → data |
---|---|
priority: | undecided → low |
All jQuery getters/setters check for undefined. Retrieving an item specifically set to undefined and trying to retrieve one that doesn't exist will produce exactly the same result, so there shouldn't be a reason to set data to undefined. I say use removeData.
Changed September 14, 2011 12:53AM UTC by comment:4
description: | Example here: \ http://jsfiddle.net/jsoverson/E25eF/4/ \ \ The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain. \ \ This issue is also what likely triggered ticket http://bugs.jquery.com/ticket/9977 but that ticket was closed prematurely due to the inability to reproduce. \ \ → Example here: \ \ \ The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain. \ \ This issue is also what likely triggered ticket but that ticket was closed prematurely due to the inability to reproduce. \ \ |
---|
-1 This request has been made before for other methods and we've argued the color of the explicit undefined
parameter bikeshed plenty of times.
Changed September 19, 2011 04:07PM UTC by comment:5
resolution: | → wontfix |
---|---|
status: | new → closed |
Changed February 14, 2012 10:17PM UTC by comment:6
While I understand the decision to not fix this issue, the documentation for .removeData() explicitly says setting value to undefined is one of the preferred ways of ensuring that the data value is not reset to the value of the data- attribute.
jQuery.fn.data appears to check for the presence of the
value
argument by the expressionvalue !== undefined
. Another way would be to check thearguments.length
, but if that was done then$(el).data('a', undefined)
would appear to seta
toundefined
, but it doesn't (jQuery.data
also uses thearg === undefined
test). So, ifa
was already set to123
, it'll remain that way. IMO, this'd be misleading. The question is: should passing an argument, even if it'sundefined
, still be construed as intentionally passing that argument, or alternatively: willfully accessing the getter functionality ofjQuery.fn.data
??What should
jQuery(el).data('foo', undefined)
be expected to do?Anyway, like I said, a fix would be easy enough -- checking for the appropriate argument-number in
arguments
.