瀏覽代碼

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

alexweissman 7 年之前
父節點
當前提交
1864a3d538
共有 3 個文件被更改,包括 24 次插入25 次删除
  1. 1 0
      CHANGELOG.md
  2. 2 2
      src/js/select2/core.js
  3. 21 23
      src/js/select2/utils.js

+ 1 - 0
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

+ 2 - 2
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');

+ 21 - 23
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];
     }
   };