1
0
mirror of synced 2025-02-09 16:49:24 +03:00

Remove jQuery from building the options

This fixes many of the speed issues the results had when working
with large data sets. jQuery has been completely dropped, with the
exception of setting the data, which does not require a jQuery
object but instead works directly with the DOM. This does not
include options with children, which still uses jQuery to deal
with the nested objects.

This only works with IE 8+, which is fine.
This commit is contained in:
Kevin Brown 2014-11-07 19:30:45 -05:00
parent 8ecc35d504
commit e3deb3ae03
8 changed files with 113 additions and 43 deletions

View File

@ -237,7 +237,8 @@ define('select2/results',[
$options.each(function () {
var $option = $(this);
var item = $option.data('data');
var item = $.data(this, 'data');
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
$option.attr('aria-selected', 'true');
@ -288,10 +289,15 @@ define('select2/results',[
delete attrs['aria-selected'];
}
var $option = $(option);
$option.attr(attrs);
for (var attr in attrs) {
var val = attrs[attr];
option.setAttribute(attr, val);
}
if (data.children) {
var $option = $(option);
var label = document.createElement('strong');
label.className = 'group-label';
@ -318,9 +324,9 @@ define('select2/results',[
this.template(data, option);
}
$option.data('data', data);
$.data(option, 'data', data);
return $option;
return option;
};
Results.prototype.bind = function (container, $container) {
@ -1113,7 +1119,7 @@ define('select2/data/select',[
var normalizedData = this._normalizeItem(data);
// Override the option's data with the combined data
$.data($option, normalizedData);
$.data(option, 'data', normalizedData);
return $option;
};
@ -1121,7 +1127,7 @@ define('select2/data/select',[
SelectAdapter.prototype.item = function ($option) {
var data = {};
data = $option.data('data');
data = $.data($option[0], 'data');
if (data != null) {
return data;
@ -1155,7 +1161,7 @@ define('select2/data/select',[
data = this._normalizeItem(data);
$option.data('data', data);
$.data($option[0], 'data', data);
return data;
};
@ -1166,6 +1172,14 @@ define('select2/data/select',[
disabled: false
};
if (item.id != null) {
item.id = item.id.toString();
}
if (item.text != null) {
item.text = item.text.toString();
}
if (item._resultId == null && item.id && this.container != null) {
item._resultId = this.generateResultId(this.container, item);
}

View File

@ -237,7 +237,8 @@ define('select2/results',[
$options.each(function () {
var $option = $(this);
var item = $option.data('data');
var item = $.data(this, 'data');
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
$option.attr('aria-selected', 'true');
@ -288,10 +289,15 @@ define('select2/results',[
delete attrs['aria-selected'];
}
var $option = $(option);
$option.attr(attrs);
for (var attr in attrs) {
var val = attrs[attr];
option.setAttribute(attr, val);
}
if (data.children) {
var $option = $(option);
var label = document.createElement('strong');
label.className = 'group-label';
@ -318,9 +324,9 @@ define('select2/results',[
this.template(data, option);
}
$option.data('data', data);
$.data(option, 'data', data);
return $option;
return option;
};
Results.prototype.bind = function (container, $container) {
@ -1113,7 +1119,7 @@ define('select2/data/select',[
var normalizedData = this._normalizeItem(data);
// Override the option's data with the combined data
$.data($option, normalizedData);
$.data(option, 'data', normalizedData);
return $option;
};
@ -1121,7 +1127,7 @@ define('select2/data/select',[
SelectAdapter.prototype.item = function ($option) {
var data = {};
data = $option.data('data');
data = $.data($option[0], 'data');
if (data != null) {
return data;
@ -1155,7 +1161,7 @@ define('select2/data/select',[
data = this._normalizeItem(data);
$option.data('data', data);
$.data($option[0], 'data', data);
return data;
};
@ -1166,6 +1172,14 @@ define('select2/data/select',[
disabled: false
};
if (item.id != null) {
item.id = item.id.toString();
}
if (item.text != null) {
item.text = item.text.toString();
}
if (item._resultId == null && item.id && this.container != null) {
item._resultId = this.generateResultId(this.container, item);
}

View File

@ -9772,7 +9772,8 @@ define('select2/results',[
$options.each(function () {
var $option = $(this);
var item = $option.data('data');
var item = $.data(this, 'data');
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
$option.attr('aria-selected', 'true');
@ -9823,10 +9824,15 @@ define('select2/results',[
delete attrs['aria-selected'];
}
var $option = $(option);
$option.attr(attrs);
for (var attr in attrs) {
var val = attrs[attr];
option.setAttribute(attr, val);
}
if (data.children) {
var $option = $(option);
var label = document.createElement('strong');
label.className = 'group-label';
@ -9853,9 +9859,9 @@ define('select2/results',[
this.template(data, option);
}
$option.data('data', data);
$.data(option, 'data', data);
return $option;
return option;
};
Results.prototype.bind = function (container, $container) {
@ -10648,7 +10654,7 @@ define('select2/data/select',[
var normalizedData = this._normalizeItem(data);
// Override the option's data with the combined data
$.data($option, normalizedData);
$.data(option, 'data', normalizedData);
return $option;
};
@ -10656,7 +10662,7 @@ define('select2/data/select',[
SelectAdapter.prototype.item = function ($option) {
var data = {};
data = $option.data('data');
data = $.data($option[0], 'data');
if (data != null) {
return data;
@ -10690,7 +10696,7 @@ define('select2/data/select',[
data = this._normalizeItem(data);
$option.data('data', data);
$.data($option[0], 'data', data);
return data;
};
@ -10701,6 +10707,14 @@ define('select2/data/select',[
disabled: false
};
if (item.id != null) {
item.id = item.id.toString();
}
if (item.text != null) {
item.text = item.text.toString();
}
if (item._resultId == null && item.id && this.container != null) {
item._resultId = this.generateResultId(this.container, item);
}

File diff suppressed because one or more lines are too long

30
dist/js/select2.js vendored
View File

@ -665,7 +665,8 @@ define('select2/results',[
$options.each(function () {
var $option = $(this);
var item = $option.data('data');
var item = $.data(this, 'data');
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
$option.attr('aria-selected', 'true');
@ -716,10 +717,15 @@ define('select2/results',[
delete attrs['aria-selected'];
}
var $option = $(option);
$option.attr(attrs);
for (var attr in attrs) {
var val = attrs[attr];
option.setAttribute(attr, val);
}
if (data.children) {
var $option = $(option);
var label = document.createElement('strong');
label.className = 'group-label';
@ -746,9 +752,9 @@ define('select2/results',[
this.template(data, option);
}
$option.data('data', data);
$.data(option, 'data', data);
return $option;
return option;
};
Results.prototype.bind = function (container, $container) {
@ -1541,7 +1547,7 @@ define('select2/data/select',[
var normalizedData = this._normalizeItem(data);
// Override the option's data with the combined data
$.data($option, normalizedData);
$.data(option, 'data', normalizedData);
return $option;
};
@ -1549,7 +1555,7 @@ define('select2/data/select',[
SelectAdapter.prototype.item = function ($option) {
var data = {};
data = $option.data('data');
data = $.data($option[0], 'data');
if (data != null) {
return data;
@ -1583,7 +1589,7 @@ define('select2/data/select',[
data = this._normalizeItem(data);
$option.data('data', data);
$.data($option[0], 'data', data);
return data;
};
@ -1594,6 +1600,14 @@ define('select2/data/select',[
disabled: false
};
if (item.id != null) {
item.id = item.id.toString();
}
if (item.text != null) {
item.text = item.text.toString();
}
if (item._resultId == null && item.id && this.container != null) {
item._resultId = this.generateResultId(this.container, item);
}

File diff suppressed because one or more lines are too long

View File

@ -140,7 +140,7 @@ define([
var normalizedData = this._normalizeItem(data);
// Override the option's data with the combined data
$.data($option, normalizedData);
$.data(option, 'data', normalizedData);
return $option;
};
@ -148,7 +148,7 @@ define([
SelectAdapter.prototype.item = function ($option) {
var data = {};
data = $option.data('data');
data = $.data($option[0], 'data');
if (data != null) {
return data;
@ -182,7 +182,7 @@ define([
data = this._normalizeItem(data);
$option.data('data', data);
$.data($option[0], 'data', data);
return data;
};
@ -193,6 +193,14 @@ define([
disabled: false
};
if (item.id != null) {
item.id = item.id.toString();
}
if (item.text != null) {
item.text = item.text.toString();
}
if (item._resultId == null && item.id && this.container != null) {
item._resultId = this.generateResultId(this.container, item);
}

View File

@ -83,7 +83,8 @@ define([
$options.each(function () {
var $option = $(this);
var item = $option.data('data');
var item = $.data(this, 'data');
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
$option.attr('aria-selected', 'true');
@ -134,10 +135,15 @@ define([
delete attrs['aria-selected'];
}
var $option = $(option);
$option.attr(attrs);
for (var attr in attrs) {
var val = attrs[attr];
option.setAttribute(attr, val);
}
if (data.children) {
var $option = $(option);
var label = document.createElement('strong');
label.className = 'group-label';
@ -164,9 +170,9 @@ define([
this.template(data, option);
}
$option.data('data', data);
$.data(option, 'data', data);
return $option;
return option;
};
Results.prototype.bind = function (container, $container) {