-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformAjax.ts
More file actions
49 lines (46 loc) · 1.44 KB
/
formAjax.ts
File metadata and controls
49 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// <reference path="formAjaxJQuery.d.ts"/>
import * as $ from "jquery";
import {AjaxRequest} from "./AjaxRequest";
import Router from "./Router";
export function formAjax($this: JQuery, settings: AjaxSettings){
const $btn = $("[type=submit]", $this);
$btn.data("orghtml", $btn.html());
$btn.html('<i class="fa fa-spinner fa-spin"></i>');
$btn.prop("disabled", true);
const newSettings: AjaxSettings = {};
for (const key in settings){
if (key !== "success" && key !== "error"){
newSettings[key] = settings[key];
}
}
if (!settings.hasOwnProperty("url")){
newSettings.url = Router.getAjaxFormURL($this.attr("action"));
}
if (!settings.hasOwnProperty("type")){
newSettings.type = $this.attr("method");
}
if (!settings.hasOwnProperty("data")){
newSettings.data = $this.serialize();
}
if (!settings.hasOwnProperty("dataType")){
newSettings.dataType = "json";
}
newSettings.success = (data: AjaxResponse, textStatus: string, jqXHR: JQueryXHR) => {
$btn.html($btn.data("orghtml"));
$btn.prop("disabled", false);
if (settings.hasOwnProperty("success")){
settings.success(data, textStatus, jqXHR);
}
};
newSettings.error = (error: AjaxError, jqXHR: JQueryXHR) => {
$btn.html($btn.data("orghtml"));
$btn.prop("disabled", false);
if (settings.hasOwnProperty("error")){
settings.error(error, jqXHR);
}
};
return AjaxRequest(newSettings);
}
$.fn.formAjax = function(settings: AjaxSettings){
formAjax($(this), settings);
};