1
0
mirror of synced 2024-11-22 21:16:10 +03:00

Cache objects in Utils.__cache instead of using $.data (#4346)

This commit is contained in:
alexweissman 2017-10-26 14:12:50 -04:00
parent 62c4f6332b
commit 1864a3d538
3 changed files with 24 additions and 25 deletions

View File

@ -13,6 +13,7 @@
- User-defined types not normalized properly when passed in as data (#4632) - User-defined types not normalized properly when passed in as data (#4632)
- Perform deep merge for `Defaults.set()` (#4364) - Perform deep merge for `Defaults.set()` (#4364)
- Fix "the results could not be loaded" displaying during AJAX request (#4356) - Fix "the results could not be loaded" displaying during AJAX request (#4356)
- Cache objects in `Utils.__cache` instead of using `$.data` (#4346)
- Removing the double event binding registration of `selection:update` (#4306) - Removing the double event binding registration of `selection:update` (#4306)
#### Accessibility #### Accessibility

View File

@ -6,7 +6,7 @@ define([
], function ($, Options, Utils, KEYS) { ], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
if (Utils.GetData($element[0], 'select2') != null) { if (Utils.GetData($element[0], 'select2') != null) {
Utils.GetData($element[0], 'select2').destroy(); Utils.GetData($element[0], 'select2').destroy();
} }
this.$element = $element; this.$element = $element;

View File

@ -74,10 +74,10 @@ define([
DecoratedClass.prototype = new ctr(); DecoratedClass.prototype = new ctr();
for (var m = 0; m < superMethods.length; m++) { for (var m = 0; m < superMethods.length; m++) {
var superMethod = superMethods[m]; var superMethod = superMethods[m];
DecoratedClass.prototype[superMethod] = DecoratedClass.prototype[superMethod] =
SuperClass.prototype[superMethod]; SuperClass.prototype[superMethod];
} }
var calledMethod = function (methodName) { var calledMethod = function (methodName) {
@ -272,10 +272,9 @@ define([
$element.append($nodes); $element.append($nodes);
}; };
// Cache objects in Utils.__cache instead of $.data // Cache objects in Utils.__cache instead of $.data (see #4346)
Utils.__cache = {}; Utils.__cache = {};
var id = 0; var id = 0;
Utils.GetUniqueElementId = function (element) { Utils.GetUniqueElementId = function (element) {
// Get a unique element Id. If element has no id, // Get a unique element Id. If element has no id,
@ -285,14 +284,14 @@ define([
var select2Id = element.getAttribute('data-select2-id'); var select2Id = element.getAttribute('data-select2-id');
if (select2Id == null) { if (select2Id == null) {
// If element has id, use it. // If element has id, use it.
if (element.id) { if (element.id) {
select2Id = element.id; select2Id = element.id;
element.setAttribute('data-select2-id', select2Id); element.setAttribute('data-select2-id', select2Id);
} else { } else {
element.setAttribute('data-select2-id', ++id); element.setAttribute('data-select2-id', ++id);
select2Id = id.toString(); select2Id = id.toString();
} }
} }
return select2Id; return select2Id;
}; };
@ -302,13 +301,12 @@ define([
// name is the cache key. // name is the cache key.
var id = Utils.GetUniqueElementId(element); var id = Utils.GetUniqueElementId(element);
if (!Utils.__cache[id]) { if (!Utils.__cache[id]) {
Utils.__cache[id] = {}; Utils.__cache[id] = {};
} }
Utils.__cache[id][name] = value; Utils.__cache[id][name] = value;
}; };
Utils.GetData = function (element, name) { Utils.GetData = function (element, name) {
// Retrieves a value from the cache by its key (name) // Retrieves a value from the cache by its key (name)
// name is optional. If no name specified, return // name is optional. If no name specified, return
@ -316,14 +314,14 @@ define([
// and for a specified element. // and for a specified element.
var id = Utils.GetUniqueElementId(element); var id = Utils.GetUniqueElementId(element);
if (name) { if (name) {
if (Utils.__cache[id]) { if (Utils.__cache[id]) {
return Utils.__cache[id][name] != null ? return Utils.__cache[id][name] != null ?
Utils.__cache[id][name]: Utils.__cache[id][name]:
$(element).data(name); // Fallback to HTML5 data attribs. $(element).data(name); // Fallback to HTML5 data attribs.
} }
return $(element).data(name); // Fallback to HTML5 data attribs. return $(element).data(name); // Fallback to HTML5 data attribs.
} else { } else {
return Utils.__cache[id]; return Utils.__cache[id];
} }
}; };
@ -331,7 +329,7 @@ define([
// Removes all cached items for a specified element. // Removes all cached items for a specified element.
var id = Utils.GetUniqueElementId(element); var id = Utils.GetUniqueElementId(element);
if (Utils.__cache[id] != null) { if (Utils.__cache[id] != null) {
delete Utils.__cache[id]; delete Utils.__cache[id];
} }
}; };