|
2 | 2 | /* |
3 | 3 | * @license MIT |
4 | 4 | * |
5 | | - * Autocomplete.js v2.4.0 |
| 5 | + * Autocomplete.js v2.5.0 |
6 | 6 | * Developed by Baptiste Donaux |
7 | 7 | * http://autocomplete-js.com |
8 | 8 | * |
@@ -113,34 +113,52 @@ var AutoComplete = (function () { |
113 | 113 | } |
114 | 114 | } |
115 | 115 | }; |
116 | | - AutoComplete.prototype.ajax = function (params, callback, timeout) { |
| 116 | + AutoComplete.prototype.makeRequest = function (params, callback) { |
| 117 | + var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders), request = new XMLHttpRequest(), method = params._HttpMethod(), url = params._Url(), queryParams = params._Pre(), queryParamsStringify = params._QueryArg() + "=" + queryParams; |
| 118 | + if (method.match(/^GET$/i)) { |
| 119 | + if (url.indexOf("?") !== -1) { |
| 120 | + url += "&" + queryParamsStringify; |
| 121 | + } |
| 122 | + else { |
| 123 | + url += "?" + queryParamsStringify; |
| 124 | + } |
| 125 | + } |
| 126 | + request.open(method, url, true); |
| 127 | + for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) { |
| 128 | + request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]); |
| 129 | + } |
| 130 | + request.onreadystatechange = function () { |
| 131 | + if (request.readyState == 4 && request.status == 200) { |
| 132 | + params.$Cache[queryParams] = request.response; |
| 133 | + callback(request.response); |
| 134 | + } |
| 135 | + }; |
| 136 | + return request; |
| 137 | + }; |
| 138 | + AutoComplete.prototype.ajax = function (params, request, timeout) { |
117 | 139 | if (timeout === void 0) { timeout = true; } |
118 | 140 | if (params.$AjaxTimer) { |
119 | 141 | window.clearTimeout(params.$AjaxTimer); |
120 | 142 | } |
121 | 143 | if (timeout === true) { |
122 | | - params.$AjaxTimer = window.setTimeout(AutoComplete.prototype.ajax.bind(null, params, callback, false), params.Delay); |
| 144 | + params.$AjaxTimer = window.setTimeout(AutoComplete.prototype.ajax.bind(null, params, request, false), params.Delay); |
123 | 145 | } |
124 | 146 | else { |
125 | 147 | if (params.Request) { |
126 | 148 | params.Request.abort(); |
127 | 149 | } |
128 | | - var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders), method = params._HttpMethod(), url = params._Url(), queryParams = params._QueryArg() + "=" + params._Pre(); |
129 | | - if (method.match(/^GET$/i)) { |
130 | | - if (url.indexOf("?") !== -1) { |
131 | | - url += "&" + queryParams; |
132 | | - } |
133 | | - else { |
134 | | - url += "?" + queryParams; |
135 | | - } |
136 | | - } |
137 | | - params.Request = new XMLHttpRequest(); |
138 | | - params.Request.open(method, url, true); |
139 | | - for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) { |
140 | | - params.Request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]); |
141 | | - } |
142 | | - params.Request.onreadystatechange = callback; |
143 | | - params.Request.send(queryParams); |
| 150 | + params.Request = request; |
| 151 | + params.Request.send(params._QueryArg() + "=" + params._Pre()); |
| 152 | + } |
| 153 | + }; |
| 154 | + AutoComplete.prototype.cache = function (params, callback) { |
| 155 | + var response = params._Cache(params._Pre()); |
| 156 | + if (response === undefined) { |
| 157 | + var request = AutoComplete.prototype.makeRequest(params, callback); |
| 158 | + AutoComplete.prototype.ajax(params, request); |
| 159 | + } |
| 160 | + else { |
| 161 | + callback(response); |
144 | 162 | } |
145 | 163 | }; |
146 | 164 | AutoComplete.prototype.destroy = function (params) { |
@@ -260,11 +278,9 @@ AutoComplete.defaults = { |
260 | 278 | if (!oldValue || currentValue != oldValue) { |
261 | 279 | this.DOMResults.setAttribute("class", "autocomplete open"); |
262 | 280 | } |
263 | | - AutoComplete.prototype.ajax(this, function () { |
264 | | - if (this.Request.readyState == 4 && this.Request.status == 200) { |
265 | | - this._Render(this._Post(this.Request.response)); |
266 | | - this._Open(); |
267 | | - } |
| 281 | + AutoComplete.prototype.cache(this, function (response) { |
| 282 | + this._Render(this._Post(response)); |
| 283 | + this._Open(); |
268 | 284 | }.bind(this)); |
269 | 285 | } |
270 | 286 | }, |
@@ -359,6 +375,12 @@ AutoComplete.defaults = { |
359 | 375 | }, 150); |
360 | 376 | } |
361 | 377 | }, |
| 378 | + /** |
| 379 | + * Manage the cache |
| 380 | + */ |
| 381 | + _Cache: function (value) { |
| 382 | + return this.$Cache[value]; |
| 383 | + }, |
362 | 384 | /** |
363 | 385 | * Manage the open |
364 | 386 | */ |
@@ -492,6 +514,7 @@ AutoComplete.defaults = { |
492 | 514 | this.Input.setAttribute("data-autocomplete-old-value", this.Input.value); |
493 | 515 | }, |
494 | 516 | $AjaxTimer: null, |
| 517 | + $Cache: {}, |
495 | 518 | $Listeners: {} |
496 | 519 | }; |
497 | 520 | module.exports = AutoComplete; |
|
0 commit comments