-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.cachedsync.js
49 lines (47 loc) · 1.71 KB
/
backbone.cachedsync.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
// Generated by CoffeeScript 1.3.3
(function() {
(function(window) {
var $, Backbone, localStorage, _;
Backbone = window.Backbone;
_ = window._;
$ = window.$;
localStorage = window.localStorage;
return Backbone.Collection.prototype.sync = function(method, collection, options) {
var cache_data, cache_meta, jqXHR, that, url;
if (!collection.cache) {
return Backbone.sync.apply(that, arguments);
}
url = _.isFunction(collection.url) ? collection.url() : collection.url;
cache_data = JSON.parse(localStorage.getItem(url));
cache_meta = JSON.parse(localStorage.getItem(collection.cache));
that = this;
if (collection.cache) {
if (cache_data) {
return $.get(collection.cache, function(data) {
var jqXHR;
if (_.isEqual(data, cache_meta)) {
options.success.call(that, cache_data, "success", $.Deferred().resolve());
return $.Deferred().resolve();
} else {
localStorage.setItem(collection.cache, JSON.stringify(data));
jqXHR = Backbone.sync.apply(that, arguments);
jqXHR.then(function(data) {
return localStorage.setItem(url, JSON.stringify(data));
});
return jqXHR;
}
});
} else {
$.get(collection.cache, function(data) {
return localStorage.setItem(collection.cache, JSON.stringify(data));
});
jqXHR = Backbone.sync.apply(that, arguments);
jqXHR.then(function(data) {
return localStorage.setItem(url, JSON.stringify(data));
});
return jqXHR;
}
}
};
})(this);
}).call(this);