1
0
mirror of synced 2024-11-26 14:56:07 +03:00

Add back ajax.transport support

This adds back the `ajax.transport` option, which allows anyone to
use their own AJAX backend. By default, `jQuery.ajax` is used as the
default transport backend.

**Breaking change:** Instead of taking `params` that contains a
`success` and `error` keys for the callback, two new callback
parameters are passed alongside of `params` that should be called
when the request succeeds or fails.

This closes https://github.com/select2/select2/issues/2987.
This commit is contained in:
Kevin Brown 2015-01-29 09:03:06 -05:00
parent 5a0f7f5518
commit 4c563a3d06
8 changed files with 149 additions and 30 deletions

View File

@ -2652,7 +2652,7 @@ define('select2/data/ajax',[
'jquery' 'jquery'
], function (ArrayAdapter, Utils, $) { ], function (ArrayAdapter, Utils, $) {
function AjaxAdapter ($element, options) { function AjaxAdapter ($element, options) {
this.ajaxOptions = options.get('ajax'); this.ajaxOptions = this._applyDefaults(options.get('ajax'));
if (this.ajaxOptions.processResults != null) { if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults; this.processResults = this.ajaxOptions.processResults;
@ -2663,6 +2663,26 @@ define('select2/data/ajax',[
Utils.Extend(AjaxAdapter, ArrayAdapter); Utils.Extend(AjaxAdapter, ArrayAdapter);
AjaxAdapter.prototype._applyDefaults = function (options) {
var defaults = {
data: function (params) {
return {
q: params.term
};
},
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
}
};
return $.extend({}, defaults, options, true);
};
AjaxAdapter.prototype.processResults = function (results) { AjaxAdapter.prototype.processResults = function (results) {
return results; return results;
}; };
@ -2689,9 +2709,7 @@ define('select2/data/ajax',[
} }
function request () { function request () {
var $request = $.ajax(options); var $request = options.transport(options, function (data) {
$request.success(function (data) {
var results = self.processResults(data, params); var results = self.processResults(data, params);
if (console && console.error) { if (console && console.error) {
@ -2705,6 +2723,8 @@ define('select2/data/ajax',[
} }
callback(results); callback(results);
}, function () {
// TODO: Handle AJAX errors
}); });
self._request = $request; self._request = $request;

View File

@ -2652,7 +2652,7 @@ define('select2/data/ajax',[
'jquery' 'jquery'
], function (ArrayAdapter, Utils, $) { ], function (ArrayAdapter, Utils, $) {
function AjaxAdapter ($element, options) { function AjaxAdapter ($element, options) {
this.ajaxOptions = options.get('ajax'); this.ajaxOptions = this._applyDefaults(options.get('ajax'));
if (this.ajaxOptions.processResults != null) { if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults; this.processResults = this.ajaxOptions.processResults;
@ -2663,6 +2663,26 @@ define('select2/data/ajax',[
Utils.Extend(AjaxAdapter, ArrayAdapter); Utils.Extend(AjaxAdapter, ArrayAdapter);
AjaxAdapter.prototype._applyDefaults = function (options) {
var defaults = {
data: function (params) {
return {
q: params.term
};
},
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
}
};
return $.extend({}, defaults, options, true);
};
AjaxAdapter.prototype.processResults = function (results) { AjaxAdapter.prototype.processResults = function (results) {
return results; return results;
}; };
@ -2689,9 +2709,7 @@ define('select2/data/ajax',[
} }
function request () { function request () {
var $request = $.ajax(options); var $request = options.transport(options, function (data) {
$request.success(function (data) {
var results = self.processResults(data, params); var results = self.processResults(data, params);
if (console && console.error) { if (console && console.error) {
@ -2705,6 +2723,8 @@ define('select2/data/ajax',[
} }
callback(results); callback(results);
}, function () {
// TODO: Handle AJAX errors
}); });
self._request = $request; self._request = $request;

View File

@ -3090,7 +3090,7 @@ define('select2/data/ajax',[
'jquery' 'jquery'
], function (ArrayAdapter, Utils, $) { ], function (ArrayAdapter, Utils, $) {
function AjaxAdapter ($element, options) { function AjaxAdapter ($element, options) {
this.ajaxOptions = options.get('ajax'); this.ajaxOptions = this._applyDefaults(options.get('ajax'));
if (this.ajaxOptions.processResults != null) { if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults; this.processResults = this.ajaxOptions.processResults;
@ -3101,6 +3101,26 @@ define('select2/data/ajax',[
Utils.Extend(AjaxAdapter, ArrayAdapter); Utils.Extend(AjaxAdapter, ArrayAdapter);
AjaxAdapter.prototype._applyDefaults = function (options) {
var defaults = {
data: function (params) {
return {
q: params.term
};
},
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
}
};
return $.extend({}, defaults, options, true);
};
AjaxAdapter.prototype.processResults = function (results) { AjaxAdapter.prototype.processResults = function (results) {
return results; return results;
}; };
@ -3127,9 +3147,7 @@ define('select2/data/ajax',[
} }
function request () { function request () {
var $request = $.ajax(options); var $request = options.transport(options, function (data) {
$request.success(function (data) {
var results = self.processResults(data, params); var results = self.processResults(data, params);
if (console && console.error) { if (console && console.error) {
@ -3143,6 +3161,8 @@ define('select2/data/ajax',[
} }
callback(results); callback(results);
}, function () {
// TODO: Handle AJAX errors
}); });
self._request = $request; self._request = $request;

File diff suppressed because one or more lines are too long

28
dist/js/select2.js vendored
View File

@ -3090,7 +3090,7 @@ define('select2/data/ajax',[
'jquery' 'jquery'
], function (ArrayAdapter, Utils, $) { ], function (ArrayAdapter, Utils, $) {
function AjaxAdapter ($element, options) { function AjaxAdapter ($element, options) {
this.ajaxOptions = options.get('ajax'); this.ajaxOptions = this._applyDefaults(options.get('ajax'));
if (this.ajaxOptions.processResults != null) { if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults; this.processResults = this.ajaxOptions.processResults;
@ -3101,6 +3101,26 @@ define('select2/data/ajax',[
Utils.Extend(AjaxAdapter, ArrayAdapter); Utils.Extend(AjaxAdapter, ArrayAdapter);
AjaxAdapter.prototype._applyDefaults = function (options) {
var defaults = {
data: function (params) {
return {
q: params.term
};
},
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
}
};
return $.extend({}, defaults, options, true);
};
AjaxAdapter.prototype.processResults = function (results) { AjaxAdapter.prototype.processResults = function (results) {
return results; return results;
}; };
@ -3127,9 +3147,7 @@ define('select2/data/ajax',[
} }
function request () { function request () {
var $request = $.ajax(options); var $request = options.transport(options, function (data) {
$request.success(function (data) {
var results = self.processResults(data, params); var results = self.processResults(data, params);
if (console && console.error) { if (console && console.error) {
@ -3143,6 +3161,8 @@ define('select2/data/ajax',[
} }
callback(results); callback(results);
}, function () {
// TODO: Handle AJAX errors
}); });
self._request = $request; self._request = $request;

File diff suppressed because one or more lines are too long

View File

@ -88,12 +88,12 @@ $("select").select2({
</p> </p>
<pre class="prettyprint linenums"> <pre class="prettyprint linenums">
$("select").select2({ $("select").select2({
ajax: { ajax: {
url: "http://example.org/api/test", url: "http://example.org/api/test",
cache: "true" cache: "true"
} }
}); });
</pre> </pre>
<p> <p>
@ -562,6 +562,25 @@ ajax: {
return { return {
results: data results: data
}; };
},
// You can use a custom AJAX transport function if you do not want to use the
// default one provided by jQuery.
//
// @param params The object containing the parameters used to generate the
// request.
// @param success A callback function that takes `data`, the results from the
// request.
// @param failure A callback function that indicates that the request could
// not be completed.
// @returns An object that has an `abort` function that can be called to abort
// the request if needed.
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
} }
} }
</pre> </pre>

View File

@ -4,7 +4,7 @@ define([
'jquery' 'jquery'
], function (ArrayAdapter, Utils, $) { ], function (ArrayAdapter, Utils, $) {
function AjaxAdapter ($element, options) { function AjaxAdapter ($element, options) {
this.ajaxOptions = options.get('ajax'); this.ajaxOptions = this._applyDefaults(options.get('ajax'));
if (this.ajaxOptions.processResults != null) { if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults; this.processResults = this.ajaxOptions.processResults;
@ -15,6 +15,26 @@ define([
Utils.Extend(AjaxAdapter, ArrayAdapter); Utils.Extend(AjaxAdapter, ArrayAdapter);
AjaxAdapter.prototype._applyDefaults = function (options) {
var defaults = {
data: function (params) {
return {
q: params.term
};
},
transport: function (params, success, failure) {
var $request = $.ajax(params);
$request.then(success);
$request.fail(failure);
return $request;
}
};
return $.extend({}, defaults, options, true);
};
AjaxAdapter.prototype.processResults = function (results) { AjaxAdapter.prototype.processResults = function (results) {
return results; return results;
}; };
@ -41,9 +61,7 @@ define([
} }
function request () { function request () {
var $request = $.ajax(options); var $request = options.transport(options, function (data) {
$request.success(function (data) {
var results = self.processResults(data, params); var results = self.processResults(data, params);
if (console && console.error) { if (console && console.error) {
@ -57,6 +75,8 @@ define([
} }
callback(results); callback(results);
}, function () {
// TODO: Handle AJAX errors
}); });
self._request = $request; self._request = $request;