From 0f30667f01f320b08b3670435c56221aacf5c22e Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 30 Aug 2014 21:14:46 -0400 Subject: [PATCH] Added very basic AJAX adapter --- dist/js/select2.amd.full.js | 52 +++++++++++++++++++- dist/js/select2.amd.js | 52 +++++++++++++++++++- dist/js/select2.full.js | 52 +++++++++++++++++++- dist/js/select2.js | 52 +++++++++++++++++++- playground/basic/adapters.html | 90 ++++++++++++++-------------------- src/js/select2/data/ajax.js | 47 ++++++++++++++++++ src/js/select2/data/array.js | 1 - src/js/select2/options.js | 3 +- 8 files changed, 286 insertions(+), 63 deletions(-) create mode 100644 src/js/select2/data/ajax.js diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index dbdfa183..778a2d89 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -568,7 +568,6 @@ define('select2/data/array',[ ], function (SelectAdapter, Utils) { function ArrayAdapter ($element, options) { this.data = options.options.data; - this.selection = []; ArrayAdapter.__super__.constructor.call(this, $element, options); } @@ -622,6 +621,54 @@ define('select2/data/array',[ return ArrayAdapter; }); +define('select2/data/ajax',[ + "./array", + "../utils", + "jquery" +], function (ArrayAdapter, Utils, $) { + function AjaxAdapter ($element, options) { + this.ajaxOptions = options.options.ajax; + + this.processResults = this.ajaxOptions.processResults || + function (results) { + return results + }; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(AjaxAdapter, ArrayAdapter); + + AjaxAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + var options = $.extend({ + type: "GET", + }, this.ajaxOptions); + + if (typeof options.url === "function") { + options.url = options.url(params); + } + + if (typeof options.data === "function") { + options.data = options.data(params); + } + + var $request = $.ajax(options); + + $request.success(function (data) { + var results = self.processResults(data); + + console.log(results) + + callback(results); + }); + }; + + return AjaxAdapter; +}); + define('select2/options',[ './data/select', './results', @@ -629,7 +676,8 @@ define('select2/options',[ './selection/single', './selection/multiple', - './data/array' + './data/array', + './data/ajax' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index dbdfa183..778a2d89 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -568,7 +568,6 @@ define('select2/data/array',[ ], function (SelectAdapter, Utils) { function ArrayAdapter ($element, options) { this.data = options.options.data; - this.selection = []; ArrayAdapter.__super__.constructor.call(this, $element, options); } @@ -622,6 +621,54 @@ define('select2/data/array',[ return ArrayAdapter; }); +define('select2/data/ajax',[ + "./array", + "../utils", + "jquery" +], function (ArrayAdapter, Utils, $) { + function AjaxAdapter ($element, options) { + this.ajaxOptions = options.options.ajax; + + this.processResults = this.ajaxOptions.processResults || + function (results) { + return results + }; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(AjaxAdapter, ArrayAdapter); + + AjaxAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + var options = $.extend({ + type: "GET", + }, this.ajaxOptions); + + if (typeof options.url === "function") { + options.url = options.url(params); + } + + if (typeof options.data === "function") { + options.data = options.data(params); + } + + var $request = $.ajax(options); + + $request.success(function (data) { + var results = self.processResults(data); + + console.log(results) + + callback(results); + }); + }; + + return AjaxAdapter; +}); + define('select2/options',[ './data/select', './results', @@ -629,7 +676,8 @@ define('select2/options',[ './selection/single', './selection/multiple', - './data/array' + './data/array', + './data/ajax' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 519a581b..0565aa39 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -10105,7 +10105,6 @@ define('select2/data/array',[ ], function (SelectAdapter, Utils) { function ArrayAdapter ($element, options) { this.data = options.options.data; - this.selection = []; ArrayAdapter.__super__.constructor.call(this, $element, options); } @@ -10159,6 +10158,54 @@ define('select2/data/array',[ return ArrayAdapter; }); +define('select2/data/ajax',[ + "./array", + "../utils", + "jquery" +], function (ArrayAdapter, Utils, $) { + function AjaxAdapter ($element, options) { + this.ajaxOptions = options.options.ajax; + + this.processResults = this.ajaxOptions.processResults || + function (results) { + return results + }; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(AjaxAdapter, ArrayAdapter); + + AjaxAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + var options = $.extend({ + type: "GET", + }, this.ajaxOptions); + + if (typeof options.url === "function") { + options.url = options.url(params); + } + + if (typeof options.data === "function") { + options.data = options.data(params); + } + + var $request = $.ajax(options); + + $request.success(function (data) { + var results = self.processResults(data); + + console.log(results) + + callback(results); + }); + }; + + return AjaxAdapter; +}); + define('select2/options',[ './data/select', './results', @@ -10166,7 +10213,8 @@ define('select2/options',[ './selection/single', './selection/multiple', - './data/array' + './data/array', + './data/ajax' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/dist/js/select2.js b/dist/js/select2.js index 7056d83f..0d8af4d3 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -996,7 +996,6 @@ define('select2/data/array',[ ], function (SelectAdapter, Utils) { function ArrayAdapter ($element, options) { this.data = options.options.data; - this.selection = []; ArrayAdapter.__super__.constructor.call(this, $element, options); } @@ -1050,6 +1049,54 @@ define('select2/data/array',[ return ArrayAdapter; }); +define('select2/data/ajax',[ + "./array", + "../utils", + "jquery" +], function (ArrayAdapter, Utils, $) { + function AjaxAdapter ($element, options) { + this.ajaxOptions = options.options.ajax; + + this.processResults = this.ajaxOptions.processResults || + function (results) { + return results + }; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(AjaxAdapter, ArrayAdapter); + + AjaxAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + var options = $.extend({ + type: "GET", + }, this.ajaxOptions); + + if (typeof options.url === "function") { + options.url = options.url(params); + } + + if (typeof options.data === "function") { + options.data = options.data(params); + } + + var $request = $.ajax(options); + + $request.success(function (data) { + var results = self.processResults(data); + + console.log(results) + + callback(results); + }); + }; + + return AjaxAdapter; +}); + define('select2/options',[ './data/select', './results', @@ -1057,7 +1104,8 @@ define('select2/options',[ './selection/single', './selection/multiple', - './data/array' + './data/array', + './data/ajax' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) { diff --git a/playground/basic/adapters.html b/playground/basic/adapters.html index d7a1601e..2466c110 100644 --- a/playground/basic/adapters.html +++ b/playground/basic/adapters.html @@ -20,61 +20,15 @@
- + + +
+ + diff --git a/src/js/select2/data/ajax.js b/src/js/select2/data/ajax.js new file mode 100644 index 00000000..4a85c4a4 --- /dev/null +++ b/src/js/select2/data/ajax.js @@ -0,0 +1,47 @@ +define([ + "./array", + "../utils", + "jquery" +], function (ArrayAdapter, Utils, $) { + function AjaxAdapter ($element, options) { + this.ajaxOptions = options.options.ajax; + + this.processResults = this.ajaxOptions.processResults || + function (results) { + return results + }; + + ArrayAdapter.__super__.constructor.call(this, $element, options); + } + + Utils.Extend(AjaxAdapter, ArrayAdapter); + + AjaxAdapter.prototype.query = function (params, callback) { + var matches = []; + var self = this; + + var options = $.extend({ + type: "GET", + }, this.ajaxOptions); + + if (typeof options.url === "function") { + options.url = options.url(params); + } + + if (typeof options.data === "function") { + options.data = options.data(params); + } + + var $request = $.ajax(options); + + $request.success(function (data) { + var results = self.processResults(data); + + console.log(results) + + callback(results); + }); + }; + + return AjaxAdapter; +}); diff --git a/src/js/select2/data/array.js b/src/js/select2/data/array.js index 5c7bf1c7..d3cc84d2 100644 --- a/src/js/select2/data/array.js +++ b/src/js/select2/data/array.js @@ -4,7 +4,6 @@ define([ ], function (SelectAdapter, Utils) { function ArrayAdapter ($element, options) { this.data = options.options.data; - this.selection = []; ArrayAdapter.__super__.constructor.call(this, $element, options); } diff --git a/src/js/select2/options.js b/src/js/select2/options.js index 0b25e462..4cfb4858 100644 --- a/src/js/select2/options.js +++ b/src/js/select2/options.js @@ -5,7 +5,8 @@ define([ './selection/single', './selection/multiple', - './data/array' + './data/array', + './data/ajax' ], function (SelectData, ResultsList, Dropdown, SingleSelection, MultipleSelection) { function Options (options) {