Skip to content

Commit 28a3608

Browse files
Add makeRequest method to simplify code
1 parent 29424bc commit 28a3608

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/autocomplete.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,36 @@ class AutoComplete {
582582
}
583583
}
584584

585+
makeRequest(params: Params, callback: any): XMLHttpRequest {
586+
var propertyHttpHeaders: string[] = Object.getOwnPropertyNames(params.HttpHeaders),
587+
request: XMLHttpRequest = new XMLHttpRequest(),
588+
method: string = params._HttpMethod(),
589+
url: string = params._Url(),
590+
queryParams: string = params._QueryArg() + "=" + params._Pre();
591+
592+
if (method.match(/^GET$/i)) {
593+
if (url.indexOf("?") !== -1) {
594+
url += "&" + queryParams;
595+
} else {
596+
url += "?" + queryParams;
597+
}
598+
}
599+
600+
request.open(method, url, true);
601+
602+
for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) {
603+
request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]);
604+
}
605+
606+
request.onreadystatechange = function() {
607+
if (request.readyState == 4 && request.status == 200) {
608+
callback(request);
609+
}
610+
};
611+
612+
return request;
613+
}
614+
585615
ajax(params: Params, callback: any, timeout: boolean = true): void {
586616
if (params.$AjaxTimer) {
587617
window.clearTimeout(params.$AjaxTimer);
@@ -594,33 +624,8 @@ class AutoComplete {
594624
params.Request.abort();
595625
}
596626

597-
var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders),
598-
method = params._HttpMethod(),
599-
url = params._Url(),
600-
queryParams = params._QueryArg() + "=" + params._Pre();
601-
602-
if (method.match(/^GET$/i)) {
603-
if (url.indexOf("?") !== -1) {
604-
url += "&" + queryParams;
605-
} else {
606-
url += "?" + queryParams;
607-
}
608-
}
609-
610-
params.Request = new XMLHttpRequest();
611-
params.Request.open(method, url, true);
612-
613-
for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) {
614-
params.Request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]);
615-
}
616-
617-
params.Request.onreadystatechange = function() {
618-
if (params.Request.readyState == 4 && params.Request.status == 200) {
619-
callback(params.Request);
620-
}
621-
};
622-
623-
params.Request.send(queryParams);
627+
params.Request = AutoComplete.prototype.makeRequest(params, callback);
628+
params.Request.send(params._QueryArg() + "=" + params._Pre());
624629
}
625630
}
626631

0 commit comments

Comments
 (0)