-
Notifications
You must be signed in to change notification settings - Fork 2
/
backbone-loading.js
38 lines (35 loc) · 1.01 KB
/
backbone-loading.js
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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('backbone'));
} else {
root.urlbuilder = factory(root.Backbone);
}
}(this, function (Backbone) {
var className = 'backbone-loading';
var onLoading = function () {
document.documentElement.classList.add(className);
};
var onComplete = function () {
document.documentElement.classList.remove(className);
};
var pendingAjaxCounter = 0;
var backboneSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
model.once('request', function (model, xhr, options) {
xhr.always(function () {
pendingAjaxCounter -= 1;
if (pendingAjaxCounter === 0) {
onComplete();
}
});
});
pendingAjaxCounter += 1;
if (pendingAjaxCounter === 1) {
onLoading();
}
return backboneSync.apply(this, arguments);
};
return Backbone;
}));