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.
This commit is contained in:
parent
e04188c85a
commit
2a7ae0ea9c
20
dist/js/select2.amd.full.js
vendored
20
dist/js/select2.amd.full.js
vendored
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
20
dist/js/select2.amd.js
vendored
20
dist/js/select2.amd.js
vendored
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
20
dist/js/select2.full.js
vendored
20
dist/js/select2.full.js
vendored
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
4
dist/js/select2.full.min.js
vendored
4
dist/js/select2.full.min.js
vendored
File diff suppressed because one or more lines are too long
20
dist/js/select2.js
vendored
20
dist/js/select2.js
vendored
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
2
dist/js/select2.min.js
vendored
2
dist/js/select2.min.js
vendored
File diff suppressed because one or more lines are too long
20
src/js/select2/defaults.js
vendored
20
src/js/select2/defaults.js
vendored
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user