blob: 7a4b5f5d6b61e6e9d3a3fb1a5d6f47d16862f7a7 (
plain)
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
|
//jules 5-May-2013
//Minotaur is a thing I wrote for brbprint that defers model updates.
//The idea is you set some keys on the model, and the Minotaur defers the actual Ajax POST/PUT.
//Also updates a save button for you, letting you know when the changes finish posting.
//
//Expects that your model has a "save" method that accepts a callback, to inform the Minotaur that the save happened.
//
//The name is a pun on Monitor but is more badass!!!! 38c (minotaur emoticon)
$(function(){
var Monitor = function () {
var base = this;
base.$el = $("#save");
base.timeout = null;
base.delay = 500;
base.objects = {};
base.init = function () {
base.$el.addClass('saved').html('Save');
base.$el.click(base.save);
}
base.watch = function (object) {
base.objects[object.type] = base.objects[object.type] || {};
base.objects[object.type][object.id] = object;
base.clear();
base.timeout = setTimeout(base.save, base.delay);
};
base.clear = function () {
if (base.timeout) clearTimeout(base.timeout);
base.timeout = false;
};
base.save = function () {
var saving = false;
base.clear();
for (var type in base.objects) {
for (var id in base.objects[type]) {
if (base.timeout)
return;
var obj = base.objects[type][id];
if (obj) obj.save(function(){
base.$el.removeClass('unsaved saving').addClass('saved').html('Saved');
saving = true;
});
base.objects[type][id] = false;
}
}
if (saving) {
base.$el.removeClass('unsaved saved').addClass('saving').html('Saving');
}
else {
base.$el.removeClass('unsaved saving').addClass('saved').html('Saved');
}
base.objects = {};
};
base.init();
};
window.Minotaur = new Monitor ();
});
|