Skip to content

Commit d127884

Browse files
authored
Add fix for $.fn.data calls
When calling `$(el).data()`, the received object contains camel-cased keys, this fixes issue jquery#436
1 parent 9010c57 commit d127884

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/jquery/data.js

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { migrateWarn } from "../main.js";
22
import { camelCase } from "../utils.js";
33

44
var oldData = jQuery.data;
5+
var oldFnData = jQuery.fn.data;
56

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

3940
return oldData.apply( this, arguments );
4041
};
42+
43+
jQuery.fn.extend({
44+
data: function(key, value) {
45+
if (arguments.length === 0 && typeof Proxy !== 'undefined') {
46+
var result = oldFnData.call(this);
47+
return new Proxy(result, {
48+
get: function(target, prop) {
49+
if (
50+
prop !== camelCase(prop) &&
51+
target[prop] === undefined
52+
) {
53+
migrateWarn(
54+
'jQuery.data() always sets/gets camelCased names: ' +
55+
prop
56+
);
57+
return target[camelCase(prop)];
58+
}
59+
return target[prop];
60+
}
61+
});
62+
}
63+
if (arguments.length > 0 && typeof key === 'string' && key !== camelCase(key)) {
64+
migrateWarn(
65+
'jQuery.data() always sets/gets camelCased names: ' + key
66+
);
67+
var args =
68+
arguments.length > 1
69+
? [camelCase(key), value]
70+
: [camelCase(key)];
71+
return oldFnData.apply(this, args);
72+
}
73+
return oldFnData.apply(this, arguments);
74+
}
75+
});

0 commit comments

Comments
 (0)