1
0
mirror of synced 2025-02-16 20:13:16 +03:00

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:
Kevin Brown 2015-01-14 21:20:18 -05:00
parent e04188c85a
commit 2a7ae0ea9c
8 changed files with 158 additions and 8 deletions

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long

20
dist/js/select2.js vendored
View File

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

File diff suppressed because one or more lines are too long

View File

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

View File

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