Skip to content

Commit e3deb3a

Browse files
committed
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.
1 parent 8ecc35d commit e3deb3a

8 files changed

Lines changed: 113 additions & 43 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ define('select2/results',[
237237

238238
$options.each(function () {
239239
var $option = $(this);
240-
var item = $option.data('data');
240+
241+
var item = $.data(this, 'data');
241242

242243
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
243244
$option.attr('aria-selected', 'true');
@@ -288,10 +289,15 @@ define('select2/results',[
288289
delete attrs['aria-selected'];
289290
}
290291

291-
var $option = $(option);
292-
$option.attr(attrs);
292+
for (var attr in attrs) {
293+
var val = attrs[attr];
294+
295+
option.setAttribute(attr, val);
296+
}
293297

294298
if (data.children) {
299+
var $option = $(option);
300+
295301
var label = document.createElement('strong');
296302
label.className = 'group-label';
297303

@@ -318,9 +324,9 @@ define('select2/results',[
318324
this.template(data, option);
319325
}
320326

321-
$option.data('data', data);
327+
$.data(option, 'data', data);
322328

323-
return $option;
329+
return option;
324330
};
325331

326332
Results.prototype.bind = function (container, $container) {
@@ -1113,15 +1119,15 @@ define('select2/data/select',[
11131119
var normalizedData = this._normalizeItem(data);
11141120

11151121
// Override the option's data with the combined data
1116-
$.data($option, normalizedData);
1122+
$.data(option, 'data', normalizedData);
11171123

11181124
return $option;
11191125
};
11201126

11211127
SelectAdapter.prototype.item = function ($option) {
11221128
var data = {};
11231129

1124-
data = $option.data('data');
1130+
data = $.data($option[0], 'data');
11251131

11261132
if (data != null) {
11271133
return data;
@@ -1155,7 +1161,7 @@ define('select2/data/select',[
11551161

11561162
data = this._normalizeItem(data);
11571163

1158-
$option.data('data', data);
1164+
$.data($option[0], 'data', data);
11591165

11601166
return data;
11611167
};
@@ -1166,6 +1172,14 @@ define('select2/data/select',[
11661172
disabled: false
11671173
};
11681174

1175+
if (item.id != null) {
1176+
item.id = item.id.toString();
1177+
}
1178+
1179+
if (item.text != null) {
1180+
item.text = item.text.toString();
1181+
}
1182+
11691183
if (item._resultId == null && item.id && this.container != null) {
11701184
item._resultId = this.generateResultId(this.container, item);
11711185
}

dist/js/select2.amd.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ define('select2/results',[
237237

238238
$options.each(function () {
239239
var $option = $(this);
240-
var item = $option.data('data');
240+
241+
var item = $.data(this, 'data');
241242

242243
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
243244
$option.attr('aria-selected', 'true');
@@ -288,10 +289,15 @@ define('select2/results',[
288289
delete attrs['aria-selected'];
289290
}
290291

291-
var $option = $(option);
292-
$option.attr(attrs);
292+
for (var attr in attrs) {
293+
var val = attrs[attr];
294+
295+
option.setAttribute(attr, val);
296+
}
293297

294298
if (data.children) {
299+
var $option = $(option);
300+
295301
var label = document.createElement('strong');
296302
label.className = 'group-label';
297303

@@ -318,9 +324,9 @@ define('select2/results',[
318324
this.template(data, option);
319325
}
320326

321-
$option.data('data', data);
327+
$.data(option, 'data', data);
322328

323-
return $option;
329+
return option;
324330
};
325331

326332
Results.prototype.bind = function (container, $container) {
@@ -1113,15 +1119,15 @@ define('select2/data/select',[
11131119
var normalizedData = this._normalizeItem(data);
11141120

11151121
// Override the option's data with the combined data
1116-
$.data($option, normalizedData);
1122+
$.data(option, 'data', normalizedData);
11171123

11181124
return $option;
11191125
};
11201126

11211127
SelectAdapter.prototype.item = function ($option) {
11221128
var data = {};
11231129

1124-
data = $option.data('data');
1130+
data = $.data($option[0], 'data');
11251131

11261132
if (data != null) {
11271133
return data;
@@ -1155,7 +1161,7 @@ define('select2/data/select',[
11551161

11561162
data = this._normalizeItem(data);
11571163

1158-
$option.data('data', data);
1164+
$.data($option[0], 'data', data);
11591165

11601166
return data;
11611167
};
@@ -1166,6 +1172,14 @@ define('select2/data/select',[
11661172
disabled: false
11671173
};
11681174

1175+
if (item.id != null) {
1176+
item.id = item.id.toString();
1177+
}
1178+
1179+
if (item.text != null) {
1180+
item.text = item.text.toString();
1181+
}
1182+
11691183
if (item._resultId == null && item.id && this.container != null) {
11701184
item._resultId = this.generateResultId(this.container, item);
11711185
}

dist/js/select2.full.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9772,7 +9772,8 @@ define('select2/results',[
97729772

97739773
$options.each(function () {
97749774
var $option = $(this);
9775-
var item = $option.data('data');
9775+
9776+
var item = $.data(this, 'data');
97769777

97779778
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
97789779
$option.attr('aria-selected', 'true');
@@ -9823,10 +9824,15 @@ define('select2/results',[
98239824
delete attrs['aria-selected'];
98249825
}
98259826

9826-
var $option = $(option);
9827-
$option.attr(attrs);
9827+
for (var attr in attrs) {
9828+
var val = attrs[attr];
9829+
9830+
option.setAttribute(attr, val);
9831+
}
98289832

98299833
if (data.children) {
9834+
var $option = $(option);
9835+
98309836
var label = document.createElement('strong');
98319837
label.className = 'group-label';
98329838

@@ -9853,9 +9859,9 @@ define('select2/results',[
98539859
this.template(data, option);
98549860
}
98559861

9856-
$option.data('data', data);
9862+
$.data(option, 'data', data);
98579863

9858-
return $option;
9864+
return option;
98599865
};
98609866

98619867
Results.prototype.bind = function (container, $container) {
@@ -10648,15 +10654,15 @@ define('select2/data/select',[
1064810654
var normalizedData = this._normalizeItem(data);
1064910655

1065010656
// Override the option's data with the combined data
10651-
$.data($option, normalizedData);
10657+
$.data(option, 'data', normalizedData);
1065210658

1065310659
return $option;
1065410660
};
1065510661

1065610662
SelectAdapter.prototype.item = function ($option) {
1065710663
var data = {};
1065810664

10659-
data = $option.data('data');
10665+
data = $.data($option[0], 'data');
1066010666

1066110667
if (data != null) {
1066210668
return data;
@@ -10690,7 +10696,7 @@ define('select2/data/select',[
1069010696

1069110697
data = this._normalizeItem(data);
1069210698

10693-
$option.data('data', data);
10699+
$.data($option[0], 'data', data);
1069410700

1069510701
return data;
1069610702
};
@@ -10701,6 +10707,14 @@ define('select2/data/select',[
1070110707
disabled: false
1070210708
};
1070310709

10710+
if (item.id != null) {
10711+
item.id = item.id.toString();
10712+
}
10713+
10714+
if (item.text != null) {
10715+
item.text = item.text.toString();
10716+
}
10717+
1070410718
if (item._resultId == null && item.id && this.container != null) {
1070510719
item._resultId = this.generateResultId(this.container, item);
1070610720
}

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ define('select2/results',[
665665

666666
$options.each(function () {
667667
var $option = $(this);
668-
var item = $option.data('data');
668+
669+
var item = $.data(this, 'data');
669670

670671
if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
671672
$option.attr('aria-selected', 'true');
@@ -716,10 +717,15 @@ define('select2/results',[
716717
delete attrs['aria-selected'];
717718
}
718719

719-
var $option = $(option);
720-
$option.attr(attrs);
720+
for (var attr in attrs) {
721+
var val = attrs[attr];
722+
723+
option.setAttribute(attr, val);
724+
}
721725

722726
if (data.children) {
727+
var $option = $(option);
728+
723729
var label = document.createElement('strong');
724730
label.className = 'group-label';
725731

@@ -746,9 +752,9 @@ define('select2/results',[
746752
this.template(data, option);
747753
}
748754

749-
$option.data('data', data);
755+
$.data(option, 'data', data);
750756

751-
return $option;
757+
return option;
752758
};
753759

754760
Results.prototype.bind = function (container, $container) {
@@ -1541,15 +1547,15 @@ define('select2/data/select',[
15411547
var normalizedData = this._normalizeItem(data);
15421548

15431549
// Override the option's data with the combined data
1544-
$.data($option, normalizedData);
1550+
$.data(option, 'data', normalizedData);
15451551

15461552
return $option;
15471553
};
15481554

15491555
SelectAdapter.prototype.item = function ($option) {
15501556
var data = {};
15511557

1552-
data = $option.data('data');
1558+
data = $.data($option[0], 'data');
15531559

15541560
if (data != null) {
15551561
return data;
@@ -1583,7 +1589,7 @@ define('select2/data/select',[
15831589

15841590
data = this._normalizeItem(data);
15851591

1586-
$option.data('data', data);
1592+
$.data($option[0], 'data', data);
15871593

15881594
return data;
15891595
};
@@ -1594,6 +1600,14 @@ define('select2/data/select',[
15941600
disabled: false
15951601
};
15961602

1603+
if (item.id != null) {
1604+
item.id = item.id.toString();
1605+
}
1606+
1607+
if (item.text != null) {
1608+
item.text = item.text.toString();
1609+
}
1610+
15971611
if (item._resultId == null && item.id && this.container != null) {
15981612
item._resultId = this.generateResultId(this.container, item);
15991613
}

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/data/select.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ define([
140140
var normalizedData = this._normalizeItem(data);
141141

142142
// Override the option's data with the combined data
143-
$.data($option, normalizedData);
143+
$.data(option, 'data', normalizedData);
144144

145145
return $option;
146146
};
147147

148148
SelectAdapter.prototype.item = function ($option) {
149149
var data = {};
150150

151-
data = $option.data('data');
151+
data = $.data($option[0], 'data');
152152

153153
if (data != null) {
154154
return data;
@@ -182,7 +182,7 @@ define([
182182

183183
data = this._normalizeItem(data);
184184

185-
$option.data('data', data);
185+
$.data($option[0], 'data', data);
186186

187187
return data;
188188
};
@@ -193,6 +193,14 @@ define([
193193
disabled: false
194194
};
195195

196+
if (item.id != null) {
197+
item.id = item.id.toString();
198+
}
199+
200+
if (item.text != null) {
201+
item.text = item.text.toString();
202+
}
203+
196204
if (item._resultId == null && item.id && this.container != null) {
197205
item._resultId = this.generateResultId(this.container, item);
198206
}

0 commit comments

Comments
 (0)