Fix bug when accessing .data() on an empty set. Return null rather than throwing exception.
This commit is contained in:
parent
1f667aa035
commit
626624a19a
|
@ -129,8 +129,8 @@ jQuery.extend({
|
||||||
|
|
||||||
jQuery.fn.extend({
|
jQuery.fn.extend({
|
||||||
data: function( key, value ) {
|
data: function( key, value ) {
|
||||||
if ( typeof key === "undefined" && this.length ) {
|
if ( typeof key === "undefined" ) {
|
||||||
return jQuery.data( this[0] );
|
return this.length ? jQuery.data( this[0] ) : null;
|
||||||
|
|
||||||
} else if ( typeof key === "object" ) {
|
} else if ( typeof key === "object" ) {
|
||||||
return this.each(function() {
|
return this.each(function() {
|
||||||
|
|
|
@ -65,7 +65,10 @@ test(".data()", function() {
|
||||||
|
|
||||||
var div = jQuery("#foo");
|
var div = jQuery("#foo");
|
||||||
div.data("test", "success");
|
div.data("test", "success");
|
||||||
same( div.data(), {test: "success"}, "data() get the entire data object" )
|
same( div.data(), {test: "success"}, "data() get the entire data object" );
|
||||||
|
|
||||||
|
var nodiv = jQuery("#unfound");
|
||||||
|
equals( nodiv.data(), null, "data() on empty set returns null" );
|
||||||
})
|
})
|
||||||
|
|
||||||
test(".data(String) and .data(String, Object)", function() {
|
test(".data(String) and .data(String, Object)", function() {
|
||||||
|
|
Loading…
Reference in a new issue