Skip to content

Commit

Permalink
Add fix for $.fn.data calls
Browse files Browse the repository at this point in the history
When calling `$(el).data()`, the received object contains camel-cased keys, this fixes issue jquery#436
  • Loading branch information
liady authored Jun 24, 2021
1 parent 9010c57 commit d127884
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/jquery/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { migrateWarn } from "../main.js";
import { camelCase } from "../utils.js";

var oldData = jQuery.data;
var oldFnData = jQuery.fn.data;

jQuery.data = function( elem, name, value ) {
var curData, sameKeys, key;
Expand Down Expand Up @@ -38,3 +39,37 @@ jQuery.data = function( elem, name, value ) {

return oldData.apply( this, arguments );
};

jQuery.fn.extend({
data: function(key, value) {
if (arguments.length === 0 && typeof Proxy !== 'undefined') {
var result = oldFnData.call(this);
return new Proxy(result, {
get: function(target, prop) {
if (
prop !== camelCase(prop) &&
target[prop] === undefined
) {
migrateWarn(
'jQuery.data() always sets/gets camelCased names: ' +
prop
);
return target[camelCase(prop)];
}
return target[prop];
}
});
}
if (arguments.length > 0 && typeof key === 'string' && key !== camelCase(key)) {
migrateWarn(
'jQuery.data() always sets/gets camelCased names: ' + key
);
var args =
arguments.length > 1
? [camelCase(key), value]
: [camelCase(key)];
return oldFnData.apply(this, args);
}
return oldFnData.apply(this, arguments);
}
});

0 comments on commit d127884

Please sign in to comment.