Skip to content

Commit

Permalink
Cache objects in Utils.__cache instead of using $.data (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweissman committed Oct 26, 2017
1 parent 62c4f63 commit 1864a3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/js/select2/core.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
44 changes: 21 additions & 23 deletions src/js/select2/utils.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
};
Expand All @@ -302,36 +301,35 @@ 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
// all cache items for the specified element.
// 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];
}
};

Utils.RemoveData = function (element) {
// 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];
}
};

Expand Down

0 comments on commit 1864a3d

Please sign in to comment.