This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
/
thorax.loading.js
227 lines (198 loc) · 6.27 KB
/
thorax.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var loadStart = 'load:start',
loadEnd = 'load:end';
/**
* load:start / load:end handler.
*
* Generates an load:start event handler that when triggered will
* then monitor the associated object for a load:end event. If the
* duration between the start and and the end events exceed
* `_loadingTimeoutDuration` then the `start` and `end` callbacks
* will be triggered at the appropriate times to allow the display
* of a loading UI.
*
* Example:
* object.bind('load:start', Thorax.loadHandler(
* function(message, background, object) {
* element.addClass('loading');
* },
* function(background, object) {
* element.removeClass('loading');
* }));
*
*/
Thorax.loadHandler = function(start, end) {
return function(message, background, object) {
var self = this;
if (!self._loadStart) {
var loadingTimeout = self._loadingTimeoutDuration;
if (loadingTimeout === void 0) {
// If we are running on a non-view object pull the default timeout
loadingTimeout = Thorax.View.prototype._loadingTimeoutDuration;
}
self._loadStart = _.extend({
events: [],
timeout: setTimeout(function() {
self._loadStart.run = true;
start.call(self, self._loadStart.message, self._loadStart.background, self._loadStart);
},
loadingTimeout*1000),
message: message,
background: background
}, Backbone.Events);
} else {
clearTimeout(self._loadStart.endTimeout);
self._loadStart.message = message;
self._loadStart.background = self._loadStart.background && background;
}
self._loadStart.events.push(object);
object.bind(loadEnd, function endCallback() {
object.unbind(loadEnd, endCallback);
var events = self._loadStart.events,
index = events.indexOf(object);
if (index >= 0) {
events.splice(index, 1);
}
if (!events.length) {
self._loadStart.endTimeout = setTimeout(function(){
if (!events.length) {
var run = self._loadStart.run;
if (run) {
// Emit the end behavior, but only if there is a paired start
end.call(self, self._loadStart.background, self._loadStart);
self._loadStart.trigger(loadEnd, self._loadStart);
}
// If stopping make sure we don't run a start
clearTimeout(self._loadStart.timeout);
self._loadStart = undefined;
}
}, 100);
}
});
};
};
/**
* Helper method for propagating load:start events to other objects.
*
* Forwards load:start events that occur on `source` to `dest`.
*/
Thorax.forwardLoadEvents = function(source, dest, once) {
function load(message, backgound, object) {
if (once) {
source.unbind(loadStart, load);
}
dest.trigger(loadStart, message, backgound, object);
}
source.bind(loadStart, load);
return {
unbind: function() {
source.unbind(loadStart, load);
}
};
};
//
// Data load event generation
//
/**
* Mixing for generating load:start and load:end events.
*/
Thorax.LoadableMixin = {
loadStart: function(message, background) {
this.trigger(loadStart, message, background, this);
},
loadEnd: function() {
this.trigger(loadEnd, this);
}
};
Thorax.sync = function(method, dataObj, options) {
var self = this,
complete = options.complete;
options.complete = function() {
self._request = undefined;
self._aborted = false;
complete && complete.apply(this, arguments);
dataObj.loadEnd();
};
dataObj.loadStart(undefined, options.background);
this._request = Backbone.sync.apply(this, arguments);
// TODO : Reevaluate this event... Seems too indepth to expose as an API
this.trigger('request', this._request);
return this._request;
};
_.each([Thorax.Collection, Thorax.Model], function(DataClass) {
var $load = DataClass.prototype.load;
_.extend(DataClass.prototype, Thorax.LoadableMixin, {
sync: Thorax.sync,
load: function(callback, failback, options) {
if (arguments.length === 2 && typeof failback !== 'function') {
options = failback;
failback = false;
}
options = options || {};
if (!options.background && !this.isPopulated()) {
// Make sure that the global scope sees the proper load events here
// if we are loading in standalone mode
Thorax.forwardLoadEvents(this, exports, true);
}
var self = this;
$load.call(this, callback,
function(isError) {
// Route changed, kill it
if (!isError) {
if (self._request) {
self._aborted = true;
self._request.abort();
}
}
failback && failback.apply && failback.apply(this, arguments);
},
options);
}
});
});
//
// View load event handling
//
_.extend(Thorax.View.prototype, {
//loading config
_loadingClassName: 'loading',
_loadingTimeoutDuration: 0.33,
setCollectionOptions: setThoraxBackgroundOptions(Thorax.View.prototype.setCollectionOptions),
setModelOptions: setThoraxBackgroundOptions(Thorax.View.prototype.setModelOptions),
onLoadStart: function(message, background, object) {
if (!this.nonBlockingLoad && !background) {
exports.trigger(loadStart, message, background, object);
}
$(this.el).addClass(this._loadingClassName);
},
onLoadEnd: function(background, object) {
$(this.el).removeClass(this._loadingClassName);
}
});
// Propagates loading view parameters to the AJAX layer
function setThoraxBackgroundOptions($super) {
return function(options) {
return $super.call(this, _.defaults({
ignoreErrors: this.ignoreFetchError,
background: this.nonBlockingLoad
}, options));
};
}
Thorax.View.registerEvents({
'load:start': Thorax.loadHandler(
function(message, background, object) {
this.onLoadStart(message, background, object);
},
function(background, object) {
this.onLoadEnd(object);
}),
collection: {
'load:start': function(message, background, object) {
this.trigger(loadStart, message, background, object);
}
},
model: {
'load:start': function(message, background, object) {
this.trigger(loadStart, message, background, object);
}
}
});