Skip to content

Commit 0d98ea0

Browse files
apepperivaynberg
authored andcommitted
Adds an additional parameter roundtripValue to function data.
Enables a stored value, which can be passed by the function 'result' to the next call of the function 'data'. This is a solution for issue select2#72 Example: data: function (term, page, roundtripValue) { var options = { q: term, limit: 5, }; if (typeof(roundtripValue) !== 'undefined' && roundtripValue != null) { options['continuation_handle'] = roundtripValue; } return options; }, results: function (data, page) { var roundtripValue = data.continuation_handle; var more = typeof(roundtripValue) !== 'undefined'; return {results: data.results, more: more, roundtripValue: roundtripValue}; }, Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
1 parent cb7f1da commit 0d98ea0

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

select2.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
* @param options object containing configuration paramters
203203
* @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
204204
* @param options.url url for the data
205-
* @param options.data a function(searchTerm, pageNumber) that should return an object containing query string parameters for the above url.
205+
* @param options.data a function(searchTerm, pageNumber, roundtripValue) that should return an object containing query string parameters for the above url.
206206
* @param options.dataType request data type: ajax, jsonp, other datatatypes supported by jQuery's $.ajax function or the transport function if specified
207207
* @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
208208
* @param options.results a function(remoteData, pageNumber) that converts data returned form the remote request to the format expected by Select2.
@@ -225,7 +225,7 @@
225225
data = options.data, // ajax data function
226226
transport = options.transport || $.ajax;
227227

228-
data = data.call(this, query.term, query.page);
228+
data = data.call(this, query.term, query.page, query.roundtripValue);
229229

230230
if( null !== handler){
231231
handler.abort();
@@ -239,7 +239,9 @@
239239
return;
240240
}
241241
// TODO 3.0 - replace query.page with query so users have access to term, page, etc.
242-
query.callback(options.results(data, query.page));
242+
var results = options.results(data, query.page);
243+
self.resultsRoundtripValue = results['roundtripValue'];
244+
query.callback(results);
243245
}
244246
});
245247
}, quietMillis);
@@ -375,6 +377,7 @@
375377
this.search = search = this.container.find("input[type=text]");
376378

377379
this.resultsPage = 0;
380+
this.resultsRoundtripValue = null;
378381

379382
// initialize the container
380383
this.initContainer();
@@ -645,7 +648,11 @@
645648

646649
if (below <= 0) {
647650
more.addClass("select2-active");
648-
this.opts.query({term: this.search.val(), page: page, callback: this.bind(function (data) {
651+
this.opts.query({
652+
term: this.search.val(),
653+
page: page,
654+
roundtripValue: self.resultsRoundtripValue,
655+
callback: this.bind(function (data) {
649656
var parts = [], self = this;
650657
$(data.results).each(function () {
651658
parts.push("<li class='select2-result'>");
@@ -691,7 +698,11 @@
691698
}
692699

693700
this.resultsPage = 1;
694-
opts.query({term: search.val(), page: this.resultsPage, callback: this.bind(function (data) {
701+
opts.query({
702+
term: search.val(),
703+
page: this.resultsPage,
704+
roundtripValue: null,
705+
callback: this.bind(function (data) {
695706
var parts = [], // html parts
696707
def; // default choice
697708

0 commit comments

Comments
 (0)