Skip to content
Permalink
Browse files

Fix #10544. Remove deprecated .data() event namespaced triggering.

Data events were horribly slow, never documented, and caused strange interpretation of data items with dots in them.
  • Loading branch information...
dmethvin committed Oct 31, 2012
1 parent 6e75fe5 commit e8cf41a051a62bf1f19beab1a5c1d643f121e28e
Showing with 7 additions and 90 deletions.
  1. +5 −23 src/data.js
  2. +1 −9 src/event.js
  3. +1 −58 test/unit/data.js
@@ -232,7 +232,7 @@ jQuery.extend({

jQuery.fn.extend({
data: function( key, value ) {
var parts, part, attr, name, l,
var attrs, name,
elem = this[0],
i = 0,
data = null;
@@ -243,9 +243,9 @@ jQuery.fn.extend({
data = jQuery.data( elem );

if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) {
attr = elem.attributes;
for ( l = attr.length; i < l; i++ ) {
name = attr[i].name;
attrs = elem.attributes;
for ( ; i < attrs.length; i++ ) {
name = attrs[i].name;

if ( !name.indexOf( "data-" ) ) {
name = jQuery.camelCase( name.substring(5) );
@@ -267,33 +267,15 @@ jQuery.fn.extend({
});
}

parts = key.split( ".", 2 );
parts[1] = parts[1] ? "." + parts[1] : "";
part = parts[1] + "!";

return jQuery.access( this, function( value ) {

if ( value === undefined ) {
data = this.triggerHandler( "getData" + part, [ parts[0] ] );

// Try to fetch any internally stored data first
if ( data === undefined && elem ) {
data = jQuery.data( elem, key );
data = dataAttr( elem, key, data );
}

return data === undefined && parts[1] ?
this.data( parts[0] ) :
data;
return elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null;
}

parts[1] = value;
this.each(function() {
var self = jQuery( this );

self.triggerHandler( "setData" + part, parts );
jQuery.data( this, key, value );
self.triggerHandler( "changeData" + part, parts );
});
}, null, value, arguments.length > 1, null, false );
},
@@ -195,14 +195,6 @@ jQuery.event = {
}
},

// Events that are safe to short-circuit if no handlers are attached.
// Native DOM events should not be added, they may have inline handlers.
customEvent: {
"getData": true,
"setData": true,
"changeData": true
},

trigger: function( event, data, elem, onlyHandlers ) {
// Don't do events on text and comment nodes
if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
@@ -232,7 +224,7 @@ jQuery.event = {
namespaces.sort();
}

if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) {
if ( !elem && !jQuery.event.global[ type ] ) {
// No jQuery handlers for this event type, and it can't have inline handlers
return;
}
@@ -175,15 +175,10 @@ test(".data()", function() {
});

test(".data(String) and .data(String, Object)", function() {
expect(29);
expect( 12 );
var parent = jQuery("<div><div></div></div>"),
div = parent.children();

parent
.bind("getData", function(){ ok( false, "getData bubbled." ); })
.bind("setData", function(){ ok( false, "setData bubbled." ); })
.bind("changeData", function(){ ok( false, "changeData bubbled." ); });

ok( div.data("test") === undefined, "Check for no data exists" );

div.data("test", "success");
@@ -199,58 +194,6 @@ test(".data(String) and .data(String, Object)", function() {

ok( div.data("notexist") === undefined, "Check for no data exists" );

div.data("test", "overwritten");
var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};


function logChangeData(e,key,value) {
var dataKey = key;
if ( e.namespace ) {
dataKey = dataKey + "." + e.namespace;
}
changes[key] += value;
changes.value = jQuery.data(e.target, dataKey);
}

div
.bind("setData",function(e,key,value){ hits[key] += value; })
.bind("setData.foo",function(e,key,value){ hits[key] += value; })
.bind("changeData",logChangeData)
.bind("changeData.foo",logChangeData)
.bind("getData",function(e,key){ gets[key] += 1; })
.bind("getData.foo",function(e,key){ gets[key] += 3; });

div.data("test.foo", 2);
equal( div.data("test"), "overwritten", "Check for original data" );
equal( div.data("test.foo"), 2, "Check for namespaced data" );
equal( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
equal( hits.test, 2, "Check triggered setter functions" );
equal( gets.test, 5, "Check triggered getter functions" );
equal( changes.test, 2, "Check sets raise changeData");
equal( changes.value, 2, "Check changeData after data has been set" );

hits.test = 0;
gets.test = 0;
changes.test = 0;
changes.value = null;

div.data("test", 1);
equal( div.data("test"), 1, "Check for original data" );
equal( div.data("test.foo"), 2, "Check for namespaced data" );
equal( div.data("test.bar"), 1, "Check for unmatched namespace" );
equal( hits.test, 1, "Check triggered setter functions" );
equal( gets.test, 5, "Check triggered getter functions" );
equal( changes.test, 1, "Check sets raise changeData" );
equal( changes.value, 1, "Check changeData after data has been set" );

div
.bind("getData",function(e,key){ return key + "root"; })
.bind("getData.foo",function(e,key){ return key + "foo"; });

equal( div.data("test"), "testroot", "Check for original data" );
equal( div.data("test.foo"), "testfoo", "Check for namespaced data" );
equal( div.data("test.bar"), "testroot", "Check for unmatched namespace" );

// #3748
var $elem = jQuery({exists:true});
equal( $elem.data("nothing"), undefined, "Non-existent data returns undefined");

0 comments on commit e8cf41a

Please sign in to comment.
You can’t perform that action at this time.