Skip to content

Commit 83b71b7

Browse files
[BUMP] 2.5.0
1 parent 5b744cf commit 83b71b7

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

dist/autocomplete.js

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* @license MIT
44
*
5-
* Autocomplete.js v2.4.0
5+
* Autocomplete.js v2.5.0
66
* Developed by Baptiste Donaux
77
* http://autocomplete-js.com
88
*
@@ -113,34 +113,52 @@ var AutoComplete = (function () {
113113
}
114114
}
115115
};
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) {
117139
if (timeout === void 0) { timeout = true; }
118140
if (params.$AjaxTimer) {
119141
window.clearTimeout(params.$AjaxTimer);
120142
}
121143
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);
123145
}
124146
else {
125147
if (params.Request) {
126148
params.Request.abort();
127149
}
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);
144162
}
145163
};
146164
AutoComplete.prototype.destroy = function (params) {
@@ -260,11 +278,9 @@ AutoComplete.defaults = {
260278
if (!oldValue || currentValue != oldValue) {
261279
this.DOMResults.setAttribute("class", "autocomplete open");
262280
}
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();
268284
}.bind(this));
269285
}
270286
},
@@ -359,6 +375,12 @@ AutoComplete.defaults = {
359375
}, 150);
360376
}
361377
},
378+
/**
379+
* Manage the cache
380+
*/
381+
_Cache: function (value) {
382+
return this.$Cache[value];
383+
},
362384
/**
363385
* Manage the open
364386
*/
@@ -492,6 +514,7 @@ AutoComplete.defaults = {
492514
this.Input.setAttribute("data-autocomplete-old-value", this.Input.value);
493515
},
494516
$AjaxTimer: null,
517+
$Cache: {},
495518
$Listeners: {}
496519
};
497520
module.exports = AutoComplete;

0 commit comments

Comments
 (0)