1
0
mirror of synced 2024-11-22 13:06:08 +03:00

Universally use the object query return

There was a plan to universally use an array as the return from
query methods, but now that we've realized pagination and extra
things are impossible without it, we're going back to the return
object. Anyone looking to migrate their data should be returning
an object with the key `results` containing what they previously
returned.

This should fix https://github.com/select2/select2/issues/2898
This commit is contained in:
Kevin Brown 2015-01-07 21:09:59 -05:00
parent 063859160d
commit 05cbaf7bf7
17 changed files with 164 additions and 133 deletions

2
dist/js/i18n/ru.js vendored
View File

@ -1 +1 @@
window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/ru",[],function(){function e(e,t,r,i){return n%10<5&&n%10>0&&n%100<5||n%100>20?n%10>1?r:t:i}return{inputTooLong:function(t){var n=t.input.length-t.maximum,r="Пожалуйста, введите еще хотя бы "+n+" символ";return r+=e(n,"","a","ов"),r},inputTooShort:function(t){var n=t.minimum-t.input.length,r="Пожалуйста, введите на "+n+" символ";return r+=e(overChars,"","a","ов"),r},loadingMore:function(){return"Загрузка данных…"},maximumSelected:function(t){var n="Вы можете выбрать не более "+t.maximum+" элемент";return n+=e(overChars,"","a","ов"),n},noResults:function(){return"Совпадений не найдено"},searching:function(){return"SПоиск…"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}();
window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/ru",[],function(){function e(e,t,n,r){return e%10<5&&e%10>0&&e%100<5||e%100>20?e%10>1?n:t:r}return{inputTooLong:function(t){var n=t.input.length-t.maximum,r="Пожалуйста, введите на "+n+" символ";return r+=e(n,"","a","ов"),r+=" меньше",r},inputTooShort:function(t){var n=t.minimum-t.input.length,r="Пожалуйста, введите еще хотя бы "+n+" символ";return r+=e(overChars,"","a","ов"),r},loadingMore:function(){return"Загрузка данных…"},maximumSelected:function(t){var n="Вы можете выбрать не более "+t.maximum+" элемент";return n+=e(overChars,"","a","ов"),n},noResults:function(){return"Совпадений не найдено"},searching:function(){return"Поиск…"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}();

View File

@ -214,7 +214,7 @@ define('select2/results',[
var $options = [];
if (data.length === 0) {
if (data.results == null || data.results.length === 0) {
if (this.$results.children().length === 0) {
this.trigger('results:message', {
message: 'noResults'
@ -224,10 +224,10 @@ define('select2/results',[
return;
}
data = this.sort(data);
data.results = this.sort(data.results);
for (var d = 0; d < data.length; d++) {
var item = data[d];
for (var d = 0; d < data.results.length; d++) {
var item = data.results[d];
var $option = this.option(item);
@ -2295,7 +2295,9 @@ define('select2/data/select',[
}
});
callback(data);
callback({
results: data
});
};
SelectAdapter.prototype.option = function (data) {
@ -2576,12 +2578,17 @@ define('select2/data/tags',[
return;
}
function wrapper (data, child) {
function wrapper (obj, child) {
var data = obj.results;
for (var i = 0; i < data.length; i++) {
var option = data[i];
var checkChildren = (
option.children != null && !wrapper(option.children, true)
option.children != null &&
!wrapper({
results: option.children
}, true)
);
var checkText = option.text === params.term;
@ -2591,7 +2598,8 @@ define('select2/data/tags',[
return false;
}
callback(data);
obj.data = data;
callback(obj);
return;
}
@ -2612,7 +2620,9 @@ define('select2/data/tags',[
self.insertTag(data, tag);
}
callback(data);
obj.results = data;
callback(obj);
}
decorated.call(this, params, wrapper);
@ -2930,7 +2940,7 @@ define('select2/dropdown/hidePlaceholder',[
}
HidePlaceholder.prototype.append = function (decorated, data) {
data = this.removePlaceholder(data);
data.results = this.removePlaceholder(data.results);
decorated.call(this, data);
};
@ -2979,12 +2989,7 @@ define('select2/dropdown/infiniteScroll',[
this.$loadingMore.remove();
this.loading = false;
if ($.isArray(data)) {
decorated.call(this, data);
return;
}
decorated.call(this, data.results);
decorated.call(this, data);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
@ -3226,7 +3231,7 @@ define('select2/dropdown/minimumResultsForSearch',[
}
MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
if (countResults(params.data) < this.minimumResultsForSearch) {
if (countResults(params.data.results) < this.minimumResultsForSearch) {
return false;
}

View File

@ -214,7 +214,7 @@ define('select2/results',[
var $options = [];
if (data.length === 0) {
if (data.results == null || data.results.length === 0) {
if (this.$results.children().length === 0) {
this.trigger('results:message', {
message: 'noResults'
@ -224,10 +224,10 @@ define('select2/results',[
return;
}
data = this.sort(data);
data.results = this.sort(data.results);
for (var d = 0; d < data.length; d++) {
var item = data[d];
for (var d = 0; d < data.results.length; d++) {
var item = data.results[d];
var $option = this.option(item);
@ -2295,7 +2295,9 @@ define('select2/data/select',[
}
});
callback(data);
callback({
results: data
});
};
SelectAdapter.prototype.option = function (data) {
@ -2576,12 +2578,17 @@ define('select2/data/tags',[
return;
}
function wrapper (data, child) {
function wrapper (obj, child) {
var data = obj.results;
for (var i = 0; i < data.length; i++) {
var option = data[i];
var checkChildren = (
option.children != null && !wrapper(option.children, true)
option.children != null &&
!wrapper({
results: option.children
}, true)
);
var checkText = option.text === params.term;
@ -2591,7 +2598,8 @@ define('select2/data/tags',[
return false;
}
callback(data);
obj.data = data;
callback(obj);
return;
}
@ -2612,7 +2620,9 @@ define('select2/data/tags',[
self.insertTag(data, tag);
}
callback(data);
obj.results = data;
callback(obj);
}
decorated.call(this, params, wrapper);
@ -2930,7 +2940,7 @@ define('select2/dropdown/hidePlaceholder',[
}
HidePlaceholder.prototype.append = function (decorated, data) {
data = this.removePlaceholder(data);
data.results = this.removePlaceholder(data.results);
decorated.call(this, data);
};
@ -2979,12 +2989,7 @@ define('select2/dropdown/infiniteScroll',[
this.$loadingMore.remove();
this.loading = false;
if ($.isArray(data)) {
decorated.call(this, data);
return;
}
decorated.call(this, data.results);
decorated.call(this, data);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
@ -3226,7 +3231,7 @@ define('select2/dropdown/minimumResultsForSearch',[
}
MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
if (countResults(params.data) < this.minimumResultsForSearch) {
if (countResults(params.data.results) < this.minimumResultsForSearch) {
return false;
}

View File

@ -9749,7 +9749,7 @@ define('select2/results',[
var $options = [];
if (data.length === 0) {
if (data.results == null || data.results.length === 0) {
if (this.$results.children().length === 0) {
this.trigger('results:message', {
message: 'noResults'
@ -9759,10 +9759,10 @@ define('select2/results',[
return;
}
data = this.sort(data);
data.results = this.sort(data.results);
for (var d = 0; d < data.length; d++) {
var item = data[d];
for (var d = 0; d < data.results.length; d++) {
var item = data.results[d];
var $option = this.option(item);
@ -11830,7 +11830,9 @@ define('select2/data/select',[
}
});
callback(data);
callback({
results: data
});
};
SelectAdapter.prototype.option = function (data) {
@ -12111,12 +12113,17 @@ define('select2/data/tags',[
return;
}
function wrapper (data, child) {
function wrapper (obj, child) {
var data = obj.results;
for (var i = 0; i < data.length; i++) {
var option = data[i];
var checkChildren = (
option.children != null && !wrapper(option.children, true)
option.children != null &&
!wrapper({
results: option.children
}, true)
);
var checkText = option.text === params.term;
@ -12126,7 +12133,8 @@ define('select2/data/tags',[
return false;
}
callback(data);
obj.data = data;
callback(obj);
return;
}
@ -12147,7 +12155,9 @@ define('select2/data/tags',[
self.insertTag(data, tag);
}
callback(data);
obj.results = data;
callback(obj);
}
decorated.call(this, params, wrapper);
@ -12465,7 +12475,7 @@ define('select2/dropdown/hidePlaceholder',[
}
HidePlaceholder.prototype.append = function (decorated, data) {
data = this.removePlaceholder(data);
data.results = this.removePlaceholder(data.results);
decorated.call(this, data);
};
@ -12514,12 +12524,7 @@ define('select2/dropdown/infiniteScroll',[
this.$loadingMore.remove();
this.loading = false;
if ($.isArray(data)) {
decorated.call(this, data);
return;
}
decorated.call(this, data.results);
decorated.call(this, data);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
@ -12761,7 +12766,7 @@ define('select2/dropdown/minimumResultsForSearch',[
}
MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
if (countResults(params.data) < this.minimumResultsForSearch) {
if (countResults(params.data.results) < this.minimumResultsForSearch) {
return false;
}

File diff suppressed because one or more lines are too long

39
dist/js/select2.js vendored
View File

@ -642,7 +642,7 @@ define('select2/results',[
var $options = [];
if (data.length === 0) {
if (data.results == null || data.results.length === 0) {
if (this.$results.children().length === 0) {
this.trigger('results:message', {
message: 'noResults'
@ -652,10 +652,10 @@ define('select2/results',[
return;
}
data = this.sort(data);
data.results = this.sort(data.results);
for (var d = 0; d < data.length; d++) {
var item = data[d];
for (var d = 0; d < data.results.length; d++) {
var item = data.results[d];
var $option = this.option(item);
@ -2723,7 +2723,9 @@ define('select2/data/select',[
}
});
callback(data);
callback({
results: data
});
};
SelectAdapter.prototype.option = function (data) {
@ -3004,12 +3006,17 @@ define('select2/data/tags',[
return;
}
function wrapper (data, child) {
function wrapper (obj, child) {
var data = obj.results;
for (var i = 0; i < data.length; i++) {
var option = data[i];
var checkChildren = (
option.children != null && !wrapper(option.children, true)
option.children != null &&
!wrapper({
results: option.children
}, true)
);
var checkText = option.text === params.term;
@ -3019,7 +3026,8 @@ define('select2/data/tags',[
return false;
}
callback(data);
obj.data = data;
callback(obj);
return;
}
@ -3040,7 +3048,9 @@ define('select2/data/tags',[
self.insertTag(data, tag);
}
callback(data);
obj.results = data;
callback(obj);
}
decorated.call(this, params, wrapper);
@ -3358,7 +3368,7 @@ define('select2/dropdown/hidePlaceholder',[
}
HidePlaceholder.prototype.append = function (decorated, data) {
data = this.removePlaceholder(data);
data.results = this.removePlaceholder(data.results);
decorated.call(this, data);
};
@ -3407,12 +3417,7 @@ define('select2/dropdown/infiniteScroll',[
this.$loadingMore.remove();
this.loading = false;
if ($.isArray(data)) {
decorated.call(this, data);
return;
}
decorated.call(this, data.results);
decorated.call(this, data);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
@ -3654,7 +3659,7 @@ define('select2/dropdown/minimumResultsForSearch',[
}
MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
if (countResults(params.data) < this.minimumResultsForSearch) {
if (countResults(params.data.results) < this.minimumResultsForSearch) {
return false;
}

File diff suppressed because one or more lines are too long

View File

@ -223,7 +223,9 @@ $(".js-data-example-ajax").select2({
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return data.items;
return {
results: data.items
};
},
cache: true
},

View File

@ -142,7 +142,9 @@ define([
}
});
callback(data);
callback({
results: data
});
};
SelectAdapter.prototype.option = function (data) {

View File

@ -28,12 +28,17 @@ define([
return;
}
function wrapper (data, child) {
function wrapper (obj, child) {
var data = obj.results;
for (var i = 0; i < data.length; i++) {
var option = data[i];
var checkChildren = (
option.children != null && !wrapper(option.children, true)
option.children != null &&
!wrapper({
results: option.children
}, true)
);
var checkText = option.text === params.term;
@ -43,7 +48,8 @@ define([
return false;
}
callback(data);
obj.data = data;
callback(obj);
return;
}
@ -64,7 +70,9 @@ define([
self.insertTag(data, tag);
}
callback(data);
obj.results = data;
callback(obj);
}
decorated.call(this, params, wrapper);

View File

@ -8,7 +8,7 @@ define([
}
HidePlaceholder.prototype.append = function (decorated, data) {
data = this.removePlaceholder(data);
data.results = this.removePlaceholder(data.results);
decorated.call(this, data);
};

View File

@ -14,12 +14,7 @@ define([
this.$loadingMore.remove();
this.loading = false;
if ($.isArray(data)) {
decorated.call(this, data);
return;
}
decorated.call(this, data.results);
decorated.call(this, data);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);

View File

@ -24,7 +24,7 @@ define([
}
MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
if (countResults(params.data) < this.minimumResultsForSearch) {
if (countResults(params.data.results) < this.minimumResultsForSearch) {
return false;
}

View File

@ -1,8 +1,9 @@
define(function () {
// Russian
function ending (count, one, couple, more) {
if (n % 10 < 5 && n % 10 > 0 && n % 100 < 5 || n % 100 > 20) {
if (n % 10 > 1) {
if (count % 10 < 5 && count % 10 > 0 &&
count % 100 < 5 || count % 100 > 20) {
if (count % 10 > 1) {
return couple;
}
} else {
@ -16,16 +17,19 @@ define(function () {
inputTooLong: function (args) {
var overChars = args.input.length - args.maximum;
var message = 'Пожалуйста, введите еще хотя бы ' + overChars + ' символ';
var message = 'Пожалуйста, введите на ' + overChars + ' символ';
message += ending(overChars, '', 'a', 'ов');
message += ' меньше';
return message;
},
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
var message = 'Пожалуйста, введите на ' + remainingChars + ' символ';
var message = 'Пожалуйста, введите еще хотя бы ' + remainingChars +
' символ';
message += ending(overChars, '', 'a', 'ов');
@ -45,7 +49,7 @@ define(function () {
return 'Совпадений не найдено';
},
searching: function () {
return 'SПоиск…';
return 'Поиск…';
}
};
});

View File

@ -50,7 +50,7 @@ define([
var $options = [];
if (data.length === 0) {
if (data.results == null || data.results.length === 0) {
if (this.$results.children().length === 0) {
this.trigger('results:message', {
message: 'noResults'
@ -60,10 +60,10 @@ define([
return;
}
data = this.sort(data);
data.results = this.sort(data.results);
for (var d = 0; d < data.length; d++) {
var item = data[d];
for (var d = 0; d < data.results.length; d++) {
var item = data.results[d];
var $option = this.option(item);

View File

@ -10,14 +10,14 @@ test('current gets default for single', function (assert) {
var data = new SelectData($select, options);
data.current(function (val) {
data.current(function (data) {
assert.equal(
val.length,
data.length,
1,
'There should only be one selected option'
);
var option = val[0];
var option = data[0];
assert.equal(
option.id,
@ -38,9 +38,9 @@ test('current gets default for multiple', function (assert) {
var data = new SelectData($select, options);
data.current(function (val) {
data.current(function (data) {
assert.equal(
val.length,
data.length,
0,
'Multiple selects have no default selection.'
);
@ -54,14 +54,14 @@ test('current gets options with explicit value', function (assert) {
$select.val('1');
data.current(function (val) {
data.current(function (data) {
assert.equal(
val.length,
data.length,
1,
'There should be one selected option'
);
var option = val[0];
var option = data[0];
assert.equal(
option.id,
@ -315,7 +315,7 @@ test('all options are returned with no term', function (assert) {
data.query({}, function (data) {
assert.equal(
data.length,
data.results.length,
3,
'The number of items returned should be equal to the number of options'
);
@ -331,7 +331,7 @@ test('the matcher checks the text', function (assert) {
term: 'Default'
}, function (data) {
assert.equal(
data.length,
data.results.length,
1,
'Only the "Default" option should be found'
);
@ -347,7 +347,7 @@ test('the matcher ignores case', function (assert) {
term: 'one'
}, function (data) {
assert.equal(
data.length,
data.results.length,
1,
'The "One" option should still be found'
);
@ -363,7 +363,7 @@ test('no options may be returned with no matches', function (assert) {
term: 'qwerty'
}, function (data) {
assert.equal(
data.length,
data.results.length,
0,
'Only matching items should be returned'
);
@ -377,7 +377,7 @@ test('optgroup tags are marked with children', function (assert) {
data.query({}, function (data) {
assert.ok(
'children' in data[0],
'children' in data.results[0],
'The optgroup element should have children when queried'
);
});
@ -390,12 +390,12 @@ test('empty optgroups are still shown when queried', function (assert) {
data.query({}, function (data) {
assert.equal(
data.length,
data.results.length,
2,
'The empty optgroup element should still be returned when queried'
);
var item = data[1];
var item = data.results[1];
assert.equal(
item.text,
@ -418,13 +418,13 @@ test('multiple options with the same value are returned', function (assert) {
data.query({}, function (data) {
assert.equal(
data.length,
data.results.length,
3,
'The duplicate option should still be returned when queried'
);
var first = data[0];
var duplicate = data[2];
var first = data.results[0];
var duplicate = data.results[2];
assert.equal(
first.id,

View File

@ -18,9 +18,9 @@ test('does not trigger on blank/null terms', function (assert) {
data.query({
term: ''
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var item = data[0];
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
@ -29,9 +29,9 @@ test('does not trigger on blank/null terms', function (assert) {
data.query({
term: null
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var item = data[0];
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
@ -44,9 +44,9 @@ test('does not trigger for additional pages', function (assert) {
data.query({
page: 2
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var item = data[0];
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
@ -59,9 +59,9 @@ test('creates tag at beginning', function (assert) {
data.query({
term: 'o'
}, function (data) {
assert.equal(data.length, 2);
assert.equal(data.results.length, 2);
var first = data[0];
var first = data.results[0];
assert.equal(first.id, 'o');
assert.equal(first.text, 'o');
@ -74,9 +74,9 @@ test('tags can be the only result', function (assert) {
data.query({
term: 'test'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var item = data[0];
var item = data.results[0];
assert.equal(item.id, 'test');
assert.equal(item.text, 'test');
@ -89,7 +89,7 @@ test('tags are injected as options', function (assert) {
data.query({
term: 'test'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
@ -108,7 +108,7 @@ test('old tags are removed automatically', function (assert) {
data.query({
term: 'first'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
@ -118,7 +118,7 @@ test('old tags are removed automatically', function (assert) {
data.query({
term: 'second'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var $children = $('#qunit-fixture .single option');
@ -141,9 +141,9 @@ test('insertTag controls the tag location', function (assert) {
data.query({
term: 'o'
}, function (data) {
assert.equal(data.length, 2);
assert.equal(data.results.length, 2);
var item = data[1];
var item = data.results[1];
assert.equal(item.id, 'o');
assert.equal(item.text, 'o');
@ -163,9 +163,9 @@ test('createTag controls the tag object', function (assert) {
data.query({
term: 'test'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
var item = data[0];
var item = data.results[0];
assert.equal(item.id, 0);
assert.equal(item.text, 'test');
@ -182,6 +182,6 @@ test('createTag returns null for no tag', function (assert) {
data.query({
term: 'o'
}, function (data) {
assert.equal(data.length, 1);
assert.equal(data.results.length, 1);
});
});