Sfoglia il codice sorgente

Better compatibility with array data

When using array data, and an option is selected, the data that is
attached to the DOM element will be run through `item` and should
have any private, automatically generated attributes added and
merged with it.
Kevin Brown 10 anni fa
parent
commit
9e130956fc

+ 14 - 4
dist/js/select2.amd.full.js

@@ -1044,10 +1044,11 @@ define('select2/data/select',[
 
 define('select2/data/array',[
   './select',
-  '../utils'
-], function (SelectAdapter, Utils) {
+  '../utils',
+  'jquery'
+], function (SelectAdapter, Utils, $) {
   function ArrayAdapter ($element, options) {
-    this.data = options.options.data;
+    this.data = options.get('data');
 
     ArrayAdapter.__super__.constructor.call(this, $element, options);
   }
@@ -1078,7 +1079,16 @@ define('select2/data/array',[
 
     $option.text(data.text);
     $option.val(data.id);
-    $option.data('data', data);
+    $option.prop('disabled', data.disabled || false);
+
+    // Get any automatically generated data values
+    var detectedData = this.item($option);
+
+    // Merge it with the already present data
+    var combinedData = $.extend({}, data, detectedData);
+
+    // Override the option's data with the combined data
+    $option.data('data', combinedData);
 
     return $option;
   };

+ 14 - 4
dist/js/select2.amd.js

@@ -1044,10 +1044,11 @@ define('select2/data/select',[
 
 define('select2/data/array',[
   './select',
-  '../utils'
-], function (SelectAdapter, Utils) {
+  '../utils',
+  'jquery'
+], function (SelectAdapter, Utils, $) {
   function ArrayAdapter ($element, options) {
-    this.data = options.options.data;
+    this.data = options.get('data');
 
     ArrayAdapter.__super__.constructor.call(this, $element, options);
   }
@@ -1078,7 +1079,16 @@ define('select2/data/array',[
 
     $option.text(data.text);
     $option.val(data.id);
-    $option.data('data', data);
+    $option.prop('disabled', data.disabled || false);
+
+    // Get any automatically generated data values
+    var detectedData = this.item($option);
+
+    // Merge it with the already present data
+    var combinedData = $.extend({}, data, detectedData);
+
+    // Override the option's data with the combined data
+    $option.data('data', combinedData);
 
     return $option;
   };

+ 14 - 4
dist/js/select2.full.js

@@ -10582,10 +10582,11 @@ define('select2/data/select',[
 
 define('select2/data/array',[
   './select',
-  '../utils'
-], function (SelectAdapter, Utils) {
+  '../utils',
+  'jquery'
+], function (SelectAdapter, Utils, $) {
   function ArrayAdapter ($element, options) {
-    this.data = options.options.data;
+    this.data = options.get('data');
 
     ArrayAdapter.__super__.constructor.call(this, $element, options);
   }
@@ -10616,7 +10617,16 @@ define('select2/data/array',[
 
     $option.text(data.text);
     $option.val(data.id);
-    $option.data('data', data);
+    $option.prop('disabled', data.disabled || false);
+
+    // Get any automatically generated data values
+    var detectedData = this.item($option);
+
+    // Merge it with the already present data
+    var combinedData = $.extend({}, data, detectedData);
+
+    // Override the option's data with the combined data
+    $option.data('data', combinedData);
 
     return $option;
   };

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.full.min.js


+ 14 - 4
dist/js/select2.js

@@ -1473,10 +1473,11 @@ define('select2/data/select',[
 
 define('select2/data/array',[
   './select',
-  '../utils'
-], function (SelectAdapter, Utils) {
+  '../utils',
+  'jquery'
+], function (SelectAdapter, Utils, $) {
   function ArrayAdapter ($element, options) {
-    this.data = options.options.data;
+    this.data = options.get('data');
 
     ArrayAdapter.__super__.constructor.call(this, $element, options);
   }
@@ -1507,7 +1508,16 @@ define('select2/data/array',[
 
     $option.text(data.text);
     $option.val(data.id);
-    $option.data('data', data);
+    $option.prop('disabled', data.disabled || false);
+
+    // Get any automatically generated data values
+    var detectedData = this.item($option);
+
+    // Merge it with the already present data
+    var combinedData = $.extend({}, data, detectedData);
+
+    // Override the option's data with the combined data
+    $option.data('data', combinedData);
 
     return $option;
   };

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.min.js


+ 14 - 4
src/js/select2/data/array.js

@@ -1,9 +1,10 @@
 define([
   './select',
-  '../utils'
-], function (SelectAdapter, Utils) {
+  '../utils',
+  'jquery'
+], function (SelectAdapter, Utils, $) {
   function ArrayAdapter ($element, options) {
-    this.data = options.options.data;
+    this.data = options.get('data');
 
     ArrayAdapter.__super__.constructor.call(this, $element, options);
   }
@@ -34,7 +35,16 @@ define([
 
     $option.text(data.text);
     $option.val(data.id);
-    $option.data('data', data);
+    $option.prop('disabled', data.disabled || false);
+
+    // Get any automatically generated data values
+    var detectedData = this.item($option);
+
+    // Merge it with the already present data
+    var combinedData = $.extend({}, data, detectedData);
+
+    // Override the option's data with the combined data
+    $option.data('data', combinedData);
 
     return $option;
   };

+ 21 - 10
tests/data/array-tests.js

@@ -60,7 +60,7 @@ test('current works with existing selections', function (assert) {
     assert.equal(
       val.length,
       1,
-      'There should only be one existing selection'
+      'There should only be one existing selection.'
     );
 
     var option = val[0];
@@ -68,13 +68,13 @@ test('current works with existing selections', function (assert) {
     assert.equal(
       option.id,
       '3',
-      'The id should be equal to the value of the option tag'
+      'The id should be equal to the value of the option tag.'
     );
 
     assert.equal(
       option.text,
       'Three',
-      'The text should be equal to the text of the option tag'
+      'The text should be equal to the text of the option tag.'
     );
   });
 });
@@ -90,13 +90,24 @@ test('current works with selected data', function (assert) {
   });
 
   data.current(function (val) {
-    assert.deepEqual(
-      val,
-      [{
-        id: '2',
-        text: '2'
-      }],
-      'The text and id should match the selected array data.'
+    assert.equal(
+      val.length,
+      1,
+      'There should only be one option selected.'
+    );
+
+    var option = val[0];
+
+    assert.equal(
+      option.id,
+      '2',
+      'The id should match the original id from the array.'
+    );
+
+    assert.equal(
+      option.text,
+      '2',
+      'The text should match the original text from the array.'
     );
   });
 });

Some files were not shown because too many files changed in this diff