Fix bug when accessing .data() on an empty set. Return null rather than throwing exception.

This commit is contained in:
Jacob Wright 2010-09-07 10:51:57 -06:00 committed by jeresig
parent 1f667aa035
commit 626624a19a
2 changed files with 18 additions and 15 deletions

View file

@ -129,8 +129,8 @@ jQuery.extend({
jQuery.fn.extend({
data: function( key, value ) {
if ( typeof key === "undefined" && this.length ) {
return jQuery.data( this[0] );
if ( typeof key === "undefined" ) {
return this.length ? jQuery.data( this[0] ) : null;
} else if ( typeof key === "object" ) {
return this.each(function() {

View file

@ -65,7 +65,10 @@ test(".data()", function() {
var div = jQuery("#foo");
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() {