浏览代码

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.
Kevin Brown 10 年之前
父节点
当前提交
e3deb3ae03

+ 22 - 8
dist/js/select2.amd.full.js

@@ -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);
     }

+ 22 - 8
dist/js/select2.amd.js

@@ -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);
     }

+ 22 - 8
dist/js/select2.full.js

@@ -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);
     }

文件差异内容过多而无法显示
+ 0 - 0
dist/js/select2.full.min.js


+ 22 - 8
dist/js/select2.js

@@ -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);
     }

文件差异内容过多而无法显示
+ 0 - 0
dist/js/select2.min.js


+ 11 - 3
src/js/select2/data/select.js

@@ -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);
     }

+ 11 - 5
src/js/select2/results.js

@@ -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) {

部分文件因为文件数量过多而无法显示