Skip to content

Commit c9d94a4

Browse files
committed
maint(pat ajax): Modernize code.
1 parent eac7bec commit c9d94a4

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/pat/ajax/ajax.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $.ajaxSetup({
2424
cache: false,
2525
});
2626

27-
var xhrCount = {};
27+
const xhrCount = {};
2828
xhrCount.get = function (a) {
2929
return this[a] !== undefined ? this[a] : 0;
3030
};
@@ -33,11 +33,11 @@ xhrCount.inc = function (a) {
3333
return this.get(a);
3434
};
3535

36-
var _ = {
36+
const _ = {
3737
name: "ajax",
3838
trigger: ".pat-ajax",
3939
parser: parser,
40-
init: function ($el) {
40+
init($el) {
4141
$el.off(".pat-ajax");
4242
$el.filter("a").on("click.pat-ajax", _.onTriggerEvents);
4343
$el.filter("form")
@@ -48,64 +48,64 @@ var _ = {
4848
});
4949
return $el;
5050
},
51-
destroy: function ($el) {
51+
destroy($el) {
5252
$el.off(".pat-ajax");
5353
},
54-
onClickSubmit: function (event) {
55-
var $form = $(event.target).parents("form").first(),
56-
name = event.target.name,
57-
value = $(event.target).val(),
58-
data = {};
54+
onClickSubmit(event) {
55+
const $form = $(event.target).parents("form").first();
56+
const name = event.target.name;
57+
const value = $(event.target).val();
58+
const data = {};
5959
if (name) {
6060
data[name] = value;
6161
}
6262
$form.data("pat-ajax.clicked-data", data);
6363
},
64-
onTriggerEvents: function (event) {
64+
onTriggerEvents(event) {
6565
if (event) {
6666
event.preventDefault();
6767
}
6868
_.request($(this));
6969
},
70-
request: function ($el, opts) {
70+
request($el, opts) {
7171
return $el.each(function () {
7272
_._request($(this), opts);
7373
});
7474
},
75-
_request: function ($el, opts) {
76-
var cfg = _.parser.parse($el, opts),
77-
onError = function (jqxhr, status, error) {
78-
// error can also stem from a javascript
79-
// exception, not only errors described in the
80-
// jqxhr.
81-
log.error("load error for " + cfg.url + ":", error, jqxhr);
75+
_request($el, opts) {
76+
const cfg = _.parser.parse($el, opts);
77+
const onError = function (jqxhr, status, error) {
78+
// error can also stem from a javascript
79+
// exception, not only errors described in the
80+
// jqxhr.
81+
log.error("load error for " + cfg.url + ":", error, jqxhr);
82+
$el.trigger({
83+
type: "pat-ajax-error",
84+
jqxhr: jqxhr,
85+
});
86+
};
87+
const seqNumber = xhrCount.inc(cfg.url);
88+
const onSuccess = function (data, status, jqxhr) {
89+
log.debug("success: jqxhr:", jqxhr);
90+
if (seqNumber === xhrCount.get(cfg.url)) {
91+
// if this url is requested multiple time, only return the last result
8292
$el.trigger({
83-
type: "pat-ajax-error",
93+
type: "pat-ajax-success",
8494
jqxhr: jqxhr,
8595
});
86-
},
87-
seqNumber = xhrCount.inc(cfg.url),
88-
onSuccess = function (data, status, jqxhr) {
89-
log.debug("success: jqxhr:", jqxhr);
90-
if (seqNumber === xhrCount.get(cfg.url)) {
91-
// if this url is requested multiple time, only return the last result
92-
$el.trigger({
93-
type: "pat-ajax-success",
94-
jqxhr: jqxhr,
95-
});
96-
} else {
97-
// ignore
98-
}
99-
},
100-
temp = $el.data("pat-ajax.clicked-data"),
101-
clickedData = temp ? $.param(temp) : "",
102-
args = {
103-
context: $el,
104-
data: [$el.serialize(), clickedData].filter(Boolean).join("&"),
105-
headers: {},
106-
url: cfg.url,
107-
method: $el.attr("method") ? $el.attr("method") : "GET",
108-
};
96+
} else {
97+
// ignore
98+
}
99+
};
100+
const temp = $el.data("pat-ajax.clicked-data");
101+
const clickedData = temp ? $.param(temp) : "";
102+
const args = {
103+
context: $el,
104+
data: [$el.serialize(), clickedData].filter(Boolean).join("&"),
105+
headers: {},
106+
url: cfg.url,
107+
method: $el.attr("method") ? $el.attr("method") : "GET",
108+
};
109109

110110
if (cfg.accept) {
111111
args.headers.Accept = cfg.accept;
@@ -116,8 +116,8 @@ var _ = {
116116
$el.attr("method") &&
117117
$el.attr("method").toUpperCase() == "POST"
118118
) {
119-
var formdata = new FormData($el[0]);
120-
for (var key in temp) {
119+
const formdata = new FormData($el[0]);
120+
for (const key in temp) {
121121
formdata.append(key, temp[key]);
122122
}
123123
args["method"] = "POST";
@@ -132,7 +132,7 @@ var _ = {
132132
log.debug("request:", args, $el[0]);
133133

134134
// Make it happen
135-
var ajax_deferred = $.ajax(args);
135+
const ajax_deferred = $.ajax(args);
136136

137137
if (ajax_deferred) ajax_deferred.done(onSuccess).fail(onError);
138138
},

0 commit comments

Comments
 (0)