소스 검색

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
8개의 변경된 파일155개의 추가작업 그리고 5개의 파일을 삭제
  1. 19 1
      dist/js/select2.amd.full.js
  2. 19 1
      dist/js/select2.amd.js
  3. 19 1
      dist/js/select2.full.js
  4. 0 0
      dist/js/select2.full.min.js
  5. 19 1
      dist/js/select2.js
  6. 0 0
      dist/js/select2.min.js
  7. 19 1
      src/js/select2/defaults.js
  8. 60 0
      tests/options/deprecated-tests.js

+ 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');
+});

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.