Ver Fonte

Fixed problems with non-string ids

We have to enforce ids being strings as the values of options within
a select will always be an id.  This fixes an issue that we had with
array selections not being highlighted in the results.
Kevin Brown há 10 anos atrás
pai
commit
27ac50a854

+ 1 - 0
Gruntfile.js

@@ -100,6 +100,7 @@ module.exports = function (grunt) {
           optimize: "none",
           name: "select2/core",
           out: "dist/js/select2.amd.js",
+          include: includes,
           paths: {
             jquery: "empty:"
           }

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

@@ -181,7 +181,9 @@ define('select2/results',[
     var self = this;
 
     this.data.current(function (selected) {
-      selected = $.map(selected, function (s) { return s.id; });
+      var selectedIds = $.map(selected, function (s) {
+        return s.id.toString();
+      });
 
       self.$results.find('.option.selected').removeClass('selected');
 
@@ -191,7 +193,7 @@ define('select2/results',[
         var $option = $(this);
         var item = $option.data('data');
 
-        if (selected.indexOf(item.id.toString()) > -1) {
+        if (selectedIds.indexOf(item.id.toString()) > -1) {
           $option.addClass('selected');
         }
       });

+ 29 - 2
dist/js/select2.amd.js

@@ -181,7 +181,9 @@ define('select2/results',[
     var self = this;
 
     this.data.current(function (selected) {
-      selected = $.map(selected, function (s) { return s.id; });
+      var selectedIds = $.map(selected, function (s) {
+        return s.id.toString();
+      });
 
       self.$results.find('.option.selected').removeClass('selected');
 
@@ -191,7 +193,7 @@ define('select2/results',[
         var $option = $(this);
         var item = $option.data('data');
 
-        if (selected.indexOf(item.id.toString()) > -1) {
+        if (selectedIds.indexOf(item.id.toString()) > -1) {
           $option.addClass('selected');
         }
       });
@@ -937,3 +939,28 @@ define('select2/core',[
   return Select2;
 });
 
+define('jquery.select2',[
+  'jquery',
+  'select2/core'
+], function ($, Select2) {
+  if ($.fn.select2 == null) {
+    $.fn.select2 = function (options) {
+      options = options || {};
+
+      if (typeof options === 'object') {
+        this.each(function () {
+          var instance = new Select2($(this), options);
+        });
+      } else if (typeof options === 'string') {
+        var instance = this.data('select2');
+
+        instance[options](arguments.slice(1));
+      } else {
+        throw new Error('Invalid arguments for Select2: ' + options);
+      }
+    };
+  }
+
+  return Select2;
+});
+

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

@@ -9719,7 +9719,9 @@ define('select2/results',[
     var self = this;
 
     this.data.current(function (selected) {
-      selected = $.map(selected, function (s) { return s.id; });
+      var selectedIds = $.map(selected, function (s) {
+        return s.id.toString();
+      });
 
       self.$results.find('.option.selected').removeClass('selected');
 
@@ -9729,7 +9731,7 @@ define('select2/results',[
         var $option = $(this);
         var item = $option.data('data');
 
-        if (selected.indexOf(item.id.toString()) > -1) {
+        if (selectedIds.indexOf(item.id.toString()) > -1) {
           $option.addClass('selected');
         }
       });

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/select2.full.min.js


+ 4 - 2
dist/js/select2.js

@@ -610,7 +610,9 @@ define('select2/results',[
     var self = this;
 
     this.data.current(function (selected) {
-      selected = $.map(selected, function (s) { return s.id; });
+      var selectedIds = $.map(selected, function (s) {
+        return s.id.toString();
+      });
 
       self.$results.find('.option.selected').removeClass('selected');
 
@@ -620,7 +622,7 @@ define('select2/results',[
         var $option = $(this);
         var item = $option.data('data');
 
-        if (selected.indexOf(item.id.toString()) > -1) {
+        if (selectedIds.indexOf(item.id.toString()) > -1) {
           $option.addClass('selected');
         }
       });

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/select2.min.js


+ 4 - 2
src/js/select2/results.js

@@ -42,7 +42,9 @@ define([
     var self = this;
 
     this.data.current(function (selected) {
-      selected = $.map(selected, function (s) { return s.id; });
+      var selectedIds = $.map(selected, function (s) {
+        return s.id.toString();
+      });
 
       self.$results.find('.option.selected').removeClass('selected');
 
@@ -52,7 +54,7 @@ define([
         var $option = $(this);
         var item = $option.data('data');
 
-        if (selected.indexOf(item.id.toString()) > -1) {
+        if (selectedIds.indexOf(item.id.toString()) > -1) {
           $option.addClass('selected');
         }
       });

+ 2 - 1
tests/data/array-tests.js

@@ -61,7 +61,8 @@ test('current works with existing selections', function (assert) {
       val,
       [{
         id: '3',
-        text: 'Three'
+        text: 'Three',
+        disabled: false
       }],
       'The text and id should match the value and text for the option tag.'
     );

+ 6 - 3
tests/data/select-tests.js

@@ -16,7 +16,8 @@ test('current gets default for single', function (assert) {
       val,
       [{
         id: 'default',
-        text: 'Default'
+        text: 'Default',
+        disabled: false
       }],
       'The first option should be selected by default (by the browser).'
     );
@@ -49,7 +50,8 @@ test('current gets options with explicit value', function (assert) {
       val,
       [{
         id: '1',
-        text: 'One'
+        text: 'One',
+        disabled: false
       }],
       'The text and id should match the value and text for the option tag.'
     );
@@ -68,7 +70,8 @@ test('current gets options with implicit value', function (assert) {
       val,
       [{
         id: '2',
-        text: '2'
+        text: '2',
+        disabled: false
       }],
       'The text and id should match the text within the option tag.'
     );

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff