-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackbone.sos.js
71 lines (55 loc) · 1.39 KB
/
backbone.sos.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Backbone.SOS
// Distributed under MIT license
// http://github.com/hernantz/backbone.sos
;(function (root, factory) {
"use strict";
if (typeof exports === "object") {
module.exports = factory(
require("underscore"),
require("backbone")
);
} else if (typeof define === "function" && define.amd) {
define([
"underscore",
"backbone"
], factory);
} else {
factory(_, Backbone);
}
}(this, function (_, Backbone) {
"use strict";
var opts = {
"loadingEvents": ["request"],
"loadedEvents": ["sync", "error"]
};
var onRequest = function (obj) {
obj.loading = true;
obj.trigger("loading", obj);
};
var onResponse = function (obj) {
obj.loading = false;
if (!obj.loaded) obj.loaded = true;
obj.trigger("loaded", obj);
};
var SOS = {
track: function (obj, options) {
options = _.extend(_.clone(opts), options);
obj.loading = false;
obj.loaded = false;
obj.on(options.loadingEvents.join(" "), onRequest, obj);
obj.on(options.loadedEvents.join(" "), onResponse, obj);
},
untrack: function (obj) {
delete obj.loading;
delete obj.loaded;
obj.off(null, onRequest);
obj.off(null, onResponse);
},
resetTracking: function (obj) {
obj.loading = false;
obj.loaded = false;
}
};
Backbone.SOS = SOS;
return Backbone.SOS;
}));