浏览代码

Add backwards compatibility with `query` option

This adds backwards compatibility with the `query` option so it
automatically patches the `DataAdapter.query` function. The only
major difference between the methods is the call signature, which
has now moved the callback out of the parameters and into the second
argument.

This also adds tests that verify that the old query functions should
work as expected.
Kevin Brown 10 年之前
父节点
当前提交
2a7ae0ea9c

+ 19 - 1
dist/js/select2.amd.full.js

@@ -3529,13 +3529,31 @@ define('select2/defaults',[
         );
       }
 
+      if (options.query != null) {
+        if (console && console.warn) {
+          console.warn(
+            'Select2: The `query` option has been deprecated in favor of a ' +
+            'custom data adapter that overrides the `query` method. Support ' +
+            'will be removed for the `query` option in future versions of ' +
+            'Select2.'
+          );
+        }
+
+        options.dataAdapter.prototype.query = function (params, callback) {
+          params.callback = callback;
+
+          options.query.call(null, params);
+        };
+      }
+
       if (options.initSelection != null) {
         if (console && console.warn) {
           console.warn(
             'Select2: The `initSelection` option has been deprecated in favor' +
             ' of a custom data adapter that overrides the `current` method. ' +
             'This method is now called multiple times instead of a single ' +
-            'time when the instance is initialized.'
+            'time when the instance is initialized. Support will be removed ' +
+            'for the `initSelection` option in future versions of Select2'
           );
         }
 

+ 19 - 1
dist/js/select2.amd.js

@@ -3529,13 +3529,31 @@ define('select2/defaults',[
         );
       }
 
+      if (options.query != null) {
+        if (console && console.warn) {
+          console.warn(
+            'Select2: The `query` option has been deprecated in favor of a ' +
+            'custom data adapter that overrides the `query` method. Support ' +
+            'will be removed for the `query` option in future versions of ' +
+            'Select2.'
+          );
+        }
+
+        options.dataAdapter.prototype.query = function (params, callback) {
+          params.callback = callback;
+
+          options.query.call(null, params);
+        };
+      }
+
       if (options.initSelection != null) {
         if (console && console.warn) {
           console.warn(
             'Select2: The `initSelection` option has been deprecated in favor' +
             ' of a custom data adapter that overrides the `current` method. ' +
             'This method is now called multiple times instead of a single ' +
-            'time when the instance is initialized.'
+            'time when the instance is initialized. Support will be removed ' +
+            'for the `initSelection` option in future versions of Select2'
           );
         }
 

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

@@ -13064,13 +13064,31 @@ define('select2/defaults',[
         );
       }
 
+      if (options.query != null) {
+        if (console && console.warn) {
+          console.warn(
+            'Select2: The `query` option has been deprecated in favor of a ' +
+            'custom data adapter that overrides the `query` method. Support ' +
+            'will be removed for the `query` option in future versions of ' +
+            'Select2.'
+          );
+        }
+
+        options.dataAdapter.prototype.query = function (params, callback) {
+          params.callback = callback;
+
+          options.query.call(null, params);
+        };
+      }
+
       if (options.initSelection != null) {
         if (console && console.warn) {
           console.warn(
             'Select2: The `initSelection` option has been deprecated in favor' +
             ' of a custom data adapter that overrides the `current` method. ' +
             'This method is now called multiple times instead of a single ' +
-            'time when the instance is initialized.'
+            'time when the instance is initialized. Support will be removed ' +
+            'for the `initSelection` option in future versions of Select2'
           );
         }
 

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


+ 19 - 1
dist/js/select2.js

@@ -3957,13 +3957,31 @@ define('select2/defaults',[
         );
       }
 
+      if (options.query != null) {
+        if (console && console.warn) {
+          console.warn(
+            'Select2: The `query` option has been deprecated in favor of a ' +
+            'custom data adapter that overrides the `query` method. Support ' +
+            'will be removed for the `query` option in future versions of ' +
+            'Select2.'
+          );
+        }
+
+        options.dataAdapter.prototype.query = function (params, callback) {
+          params.callback = callback;
+
+          options.query.call(null, params);
+        };
+      }
+
       if (options.initSelection != null) {
         if (console && console.warn) {
           console.warn(
             'Select2: The `initSelection` option has been deprecated in favor' +
             ' of a custom data adapter that overrides the `current` method. ' +
             'This method is now called multiple times instead of a single ' +
-            'time when the instance is initialized.'
+            'time when the instance is initialized. Support will be removed ' +
+            'for the `initSelection` option in future versions of Select2'
           );
         }
 

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


+ 19 - 1
src/js/select2/defaults.js

@@ -93,13 +93,31 @@ define([
         );
       }
 
+      if (options.query != null) {
+        if (console && console.warn) {
+          console.warn(
+            'Select2: The `query` option has been deprecated in favor of a ' +
+            'custom data adapter that overrides the `query` method. Support ' +
+            'will be removed for the `query` option in future versions of ' +
+            'Select2.'
+          );
+        }
+
+        options.dataAdapter.prototype.query = function (params, callback) {
+          params.callback = callback;
+
+          options.query.call(null, params);
+        };
+      }
+
       if (options.initSelection != null) {
         if (console && console.warn) {
           console.warn(
             'Select2: The `initSelection` option has been deprecated in favor' +
             ' of a custom data adapter that overrides the `current` method. ' +
             'This method is now called multiple times instead of a single ' +
-            'time when the instance is initialized.'
+            'time when the instance is initialized. Support will be removed ' +
+            'for the `initSelection` option in future versions of Select2'
           );
         }
 

+ 60 - 0
tests/options/deprecated-tests.js

@@ -156,3 +156,63 @@ test('only called once', function (assert) {
     'initSelection should have only been called once'
   );
 });
+
+module('Options - Deprecated - query');
+
+test('converted into dataAdapter.query automatically', function (assert) {
+  expect(6);
+
+  var $test = $('<select></select>');
+  var called = false;
+
+  var options = new Options({
+    query: function (params) {
+      called = true;
+
+      params.callback({
+        results: [
+          {
+            id: 'test',
+            text: params.term
+          }
+        ]
+      });
+    }
+  }, $test);
+
+  assert.ok(!called, 'The query option should not have been called');
+
+  var DataAdapter = options.get('dataAdapter');
+  var data = new DataAdapter($test, options);
+
+  data.query({
+    term: 'term'
+  }, function (data) {
+    assert.ok(
+      'results' in data,
+      'It should have included the results key'
+    );
+
+    assert.equal(
+      data.results.length,
+      1,
+      'There should have only been a single result returned'
+    );
+
+    var item = data.results[0];
+
+    assert.equal(
+      item.id,
+      'test',
+      'The id should have been returned from the query function'
+    );
+
+    assert.equal(
+      item.text,
+      'term',
+      'The text should have matched the term that was passed in'
+    );
+  });
+
+  assert.ok(called, 'The query function should have been called');
+});

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