-
Notifications
You must be signed in to change notification settings - Fork 0
/
draftable.js
41 lines (33 loc) · 1.12 KB
/
draftable.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
Backbone.Draftable = function() {
var _model = this;
var draftAttributes;
var timerId;
_.extend(this, {
saveDraft : function() {
var modelAttributesJSON = JSON.stringify(_model.attributes);
// If this is the first change, or the model has changed since the last
// draft, store in localStorage
if (!draftAttributes || JSON.stringify(draftAttributes) !== modelAttributesJSON) {
localStorage.setItem('message', modelAttributesJSON);
draftAttributes = _(_model.attributes).clone();
}
},
getDraft : function() {
return JSON.parse(localStorage.getItem("message"));
},
startMonitoring : function() {
timerId = setInterval(_model.saveDraft, 1000);
},
stopMonitoring : function() {
clearInterval(timerId);
},
restartTimer: function() {
_model.stopMonitoring();
_model.startMonitoring();
}
});
// Set the model attributes from the draft in localStorage, if available.
_model.set(_model.getDraft());
// reset timer everytime a change is made to draft
_model.listenTo(_model, 'change', _model.restartTimer);
};