-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.fetchOrQueue.js
More file actions
47 lines (37 loc) · 1.36 KB
/
backbone.fetchOrQueue.js
File metadata and controls
47 lines (37 loc) · 1.36 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
define(function(require, exports, module) {
Backbone.Model.prototype.fetchOrQueue =
Backbone.Collection.prototype.fetchOrQueue = function(callback, options) {
var resource = this;
if (!options) options = {};
if (resource.loaded && ((Date.now() - resource.loaded.getTime()) < 2*60*1000)) {
if (callback) setTimeout(function() { callback(false, resource); }, 0);
return resource;
}
if (callback) {
if (!resource._pending_callbacks)
resource._pending_callbacks = [];
resource._pending_callbacks.push(callback);
if (!resource.fetching) {
options.success = function() {
resource.loaded = new Date();
resource.fetching = false;
resource._pending_callbacks.map(function(callback){
callback(false, resource);
});
delete resource._pending_callbacks;
};
options.error = function() {
resource.loaded = false;
resource.fetching = false;
resource._pending_callbacks.map(function(callback){
callback(true);
});
delete resource._pending_callbacks;
};
resource.fetch(options);
resource.fetching = true;
}
}
return resource;
};
});