Browse Source

Allow chaining of `.select2()`

`.select2("open")`, `.select2("close")`, and `.select2("destroy")`
can now be safely chained.

This closes https://github.com/select2/select2/issues/3221.
Kevin Brown 10 năm trước cách đây
mục cha
commit
01160f29d8

+ 11 - 1
dist/js/select2.full.js

@@ -5574,6 +5574,9 @@ S2.define('jquery.select2',[
   require('jquery.mousewheel');
 
   if ($.fn.select2 == null) {
+    // All methods that should return the element
+    var thisMethods = ['open', 'close', 'destroy'];
+
     $.fn.select2 = function (options) {
       options = options || {};
 
@@ -5589,7 +5592,14 @@ S2.define('jquery.select2',[
         var instance = this.data('select2');
         var args = Array.prototype.slice.call(arguments, 1);
 
-        return instance[options](args);
+        var ret = instance[options](args);
+
+        // Check if we should be returning `this`
+        if ($.inArray(options, thisMethods) > -1) {
+          return this;
+        }
+
+        return ret;
       } else {
         throw new Error('Invalid arguments for Select2: ' + options);
       }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/js/select2.full.min.js


+ 11 - 1
dist/js/select2.js

@@ -5236,6 +5236,9 @@ S2.define('jquery.select2',[
   require('jquery.mousewheel');
 
   if ($.fn.select2 == null) {
+    // All methods that should return the element
+    var thisMethods = ['open', 'close', 'destroy'];
+
     $.fn.select2 = function (options) {
       options = options || {};
 
@@ -5251,7 +5254,14 @@ S2.define('jquery.select2',[
         var instance = this.data('select2');
         var args = Array.prototype.slice.call(arguments, 1);
 
-        return instance[options](args);
+        var ret = instance[options](args);
+
+        // Check if we should be returning `this`
+        if ($.inArray(options, thisMethods) > -1) {
+          return this;
+        }
+
+        return ret;
       } else {
         throw new Error('Invalid arguments for Select2: ' + options);
       }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/js/select2.min.js


+ 11 - 1
src/js/jquery.select2.js

@@ -9,6 +9,9 @@ define([
   require('jquery.mousewheel');
 
   if ($.fn.select2 == null) {
+    // All methods that should return the element
+    var thisMethods = ['open', 'close', 'destroy'];
+
     $.fn.select2 = function (options) {
       options = options || {};
 
@@ -24,7 +27,14 @@ define([
         var instance = this.data('select2');
         var args = Array.prototype.slice.call(arguments, 1);
 
-        return instance[options](args);
+        var ret = instance[options](args);
+
+        // Check if we should be returning `this`
+        if ($.inArray(options, thisMethods) > -1) {
+          return this;
+        }
+
+        return ret;
       } else {
         throw new Error('Invalid arguments for Select2: ' + options);
       }

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác