From 1864a3d538dd31f11af946fe5748d3bfe5034d6f Mon Sep 17 00:00:00 2001 From: alexweissman Date: Thu, 26 Oct 2017 14:12:50 -0400 Subject: [PATCH] Cache objects in `Utils.__cache` instead of using `$.data` (#4346) --- CHANGELOG.md | 1 + src/js/select2/core.js | 4 ++-- src/js/select2/utils.js | 44 ++++++++++++++++++++--------------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dce0f51b..68d4b1ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - User-defined types not normalized properly when passed in as data (#4632) - Perform deep merge for `Defaults.set()` (#4364) - 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) #### Accessibility diff --git a/src/js/select2/core.js b/src/js/select2/core.js index 61fee6ca..cd6e99b8 100644 --- a/src/js/select2/core.js +++ b/src/js/select2/core.js @@ -6,7 +6,7 @@ define([ ], function ($, Options, Utils, KEYS) { var Select2 = function ($element, options) { if (Utils.GetData($element[0], 'select2') != null) { - Utils.GetData($element[0], 'select2').destroy(); + Utils.GetData($element[0], 'select2').destroy(); } this.$element = $element; @@ -573,7 +573,7 @@ define([ this._syncS = null; this.$element.off('.select2'); - this.$element.attr('tabindex', + this.$element.attr('tabindex', Utils.GetData(this.$element[0], 'old-tabindex')); this.$element.removeClass('select2-hidden-accessible'); diff --git a/src/js/select2/utils.js b/src/js/select2/utils.js index 7905e49b..127ede73 100644 --- a/src/js/select2/utils.js +++ b/src/js/select2/utils.js @@ -74,10 +74,10 @@ define([ DecoratedClass.prototype = new ctr(); for (var m = 0; m < superMethods.length; m++) { - var superMethod = superMethods[m]; + var superMethod = superMethods[m]; - DecoratedClass.prototype[superMethod] = - SuperClass.prototype[superMethod]; + DecoratedClass.prototype[superMethod] = + SuperClass.prototype[superMethod]; } var calledMethod = function (methodName) { @@ -272,10 +272,9 @@ define([ $element.append($nodes); }; - // Cache objects in Utils.__cache instead of $.data + // Cache objects in Utils.__cache instead of $.data (see #4346) Utils.__cache = {}; - var id = 0; Utils.GetUniqueElementId = function (element) { // Get a unique element Id. If element has no id, @@ -285,14 +284,14 @@ define([ var select2Id = element.getAttribute('data-select2-id'); if (select2Id == null) { - // If element has id, use it. - if (element.id) { - select2Id = element.id; - element.setAttribute('data-select2-id', select2Id); - } else { - element.setAttribute('data-select2-id', ++id); - select2Id = id.toString(); - } + // If element has id, use it. + if (element.id) { + select2Id = element.id; + element.setAttribute('data-select2-id', select2Id); + } else { + element.setAttribute('data-select2-id', ++id); + select2Id = id.toString(); + } } return select2Id; }; @@ -302,13 +301,12 @@ define([ // name is the cache key. var id = Utils.GetUniqueElementId(element); if (!Utils.__cache[id]) { - Utils.__cache[id] = {}; + Utils.__cache[id] = {}; } Utils.__cache[id][name] = value; }; - Utils.GetData = function (element, name) { // Retrieves a value from the cache by its key (name) // name is optional. If no name specified, return @@ -316,14 +314,14 @@ define([ // and for a specified element. var id = Utils.GetUniqueElementId(element); if (name) { - if (Utils.__cache[id]) { - return Utils.__cache[id][name] != null ? - Utils.__cache[id][name]: - $(element).data(name); // Fallback to HTML5 data attribs. - } - return $(element).data(name); // Fallback to HTML5 data attribs. + if (Utils.__cache[id]) { + return Utils.__cache[id][name] != null ? + Utils.__cache[id][name]: + $(element).data(name); // Fallback to HTML5 data attribs. + } + return $(element).data(name); // Fallback to HTML5 data attribs. } else { - return Utils.__cache[id]; + return Utils.__cache[id]; } }; @@ -331,7 +329,7 @@ define([ // Removes all cached items for a specified element. var id = Utils.GetUniqueElementId(element); if (Utils.__cache[id] != null) { - delete Utils.__cache[id]; + delete Utils.__cache[id]; } };