Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#12786 closed bug (fixed)

jQuery removeData() doesn't work , it seems like a bug

Reported by: lovesueee@… Owned by: Rick Waldron
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.2
Keywords: Cc:
Blocked by: Blocking:

Description

when I use the code follow:

<script> var a = {}; $(a).data('a-a', 1); $(a).data('b-b', 2); add data to cache

$(a).removeData(['a-a','b-b']); it dosen't work $(a).removeData('a-a b-b'); it works </script> and I think the reason is here in data.js :

if ( !jQuery.isArray( name ) ) {

try the string as a key before any manipulation if ( name in thisCache ) {

name = [ name ];

} else {

split the camel cased version by spaces unless a key with the spaces exists name = jQuery.camelCase( name ); if ( name in thisCache ) {

name = [ name ];

} else {

name = name.split(" ");

}

}

}

for ( i = 0, l = name.length; i < l; i++ ) {

delete thisCache[ name[i] ];

}

when the param name is an array , we don't use jQuery.camelCase( name ) ,so we can't remove the data.

Change History (5)

comment:1 Changed 7 years ago by lovesueee@…

when I use the code follow:

<script>
var a = {};
$(a).data('a-a', 1);
$(a).data('b-b', 2); // add data to cache

$(a).removeData(['a-a','b-b']); // it dosen't work
$(a).removeData('a-a b-b'); // it works
</script>

and I think the reason is here in data.js :

if ( !jQuery.isArray( name ) ) {

	// try the string as a key before any manipulation
	if ( name in thisCache ) {
		name = [ name ];
	} else {

		// split the camel cased version by spaces unless a key with the spaces exists
		name = jQuery.camelCase( name );
		if ( name in thisCache ) {
			name = [ name ];
		} else {
			name = name.split(" ");
		}
	}
}

for ( i = 0, l = name.length; i < l; i++ ) {
	delete thisCache[ name[i] ];
}

when the param name is an array , we don't use jQuery.camelCase( name ),so we can't remove the data.

comment:2 Changed 7 years ago by dmethvin

Owner: set to lovesueee@…
Status: newpending

The only reason we support .data() on plain objects is so that events work on plain objects. Internally we never need the camel-casing that .data() provides. What is the use case there, versus just setting a property on the object?

comment:3 Changed 7 years ago by Rick Waldron

Owner: changed from lovesueee@… to Rick Waldron
Status: pendingassigned

comment:4 Changed 7 years ago by Rick Waldron

Resolution: fixed
Status: assignedclosed

Brute force property removal when removeData([a,b,c]). Fixes #12786

Changeset: 812c6087ada4a6383fd2e033d04648ec0f6691ea

Last edited 7 years ago by Rick Waldron (previous) (diff)

comment:5 Changed 7 years ago by Rick Waldron

#12836 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.