-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-tracking.js
103 lines (90 loc) · 2.85 KB
/
backbone-tracking.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
// Generated by CoffeeScript 1.3.3
(function() {
Backbone.Model.prototype.setSilently = function(attributes) {
return this.set(attributes, {
silent: true
});
};
Backbone.Model.prototype.startTracking = function(options) {
var _this = this;
this.version = 0;
this.attributeArray = [$.extend(true, {}, this.attributes)];
if ((options != null ? options.onChange : void 0) != null) {
this.on('change', (function() {
return _this.commit();
}), this.attributeArray);
}
if ((options != null ? options.onSave : void 0) != null) {
return this.on('sync', (function() {
if (!((options != null ? options.onChange : void 0) != null)) {
return _this.commit();
}
}), this.attributeArray);
}
};
Backbone.Model.prototype.stopTracking = function() {
this.off(null, null, this.attributeArray);
delete this.version;
return delete this.attributeArray;
};
Backbone.Model.prototype.commit = function() {
this.version++;
return this.attributeArray.push($.extend(true, {}, this.attributes));
};
Backbone.Model.prototype.revert = function(versionsBehind) {
var versionNumber;
if (this.version === 0) {
return;
}
versionNumber = this.version - (versionsBehind || 1);
if (versionNumber >= 0) {
this.setSilently(this.attributeArray[versionNumber]);
return this.version = versionNumber;
}
};
Backbone.Model.prototype.revertToOriginal = function() {
this.setSilently(this.attributeArray[0]);
return this.version = 0;
};
Backbone.Model.prototype.progress = function(versionsAhead) {
var versionNumber;
if (this.version === (this.attributeArray.length - 1)) {
return;
}
versionNumber = this.version + (versionsAhead || 1);
if (versionNumber < this.attributeArray.length) {
this.setSilently(this.attributeArray[versionNumber]);
return this.version = versionNumber;
}
};
Backbone.Model.prototype.progressToNewest = function() {
this.setSilently(this.attributeArray[this.attributeArray.length - 1]);
return this.version = this.attributeArray.length - 1;
};
Backbone.Model.prototype.clear = function() {
return this.setSilently(this.attributeArray[this.version]);
};
/* Searches older commits first
*/
Backbone.Model.prototype.where = function(queryObj) {
var commit, flag, key, tempVersion, value, _i, _len, _ref;
tempVersion = 0;
_ref = this.attributeArray;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
commit = _ref[_i];
flag = true;
for (key in queryObj) {
value = queryObj[key];
if (commit[key] !== value) {
flag = false;
}
}
if (flag === true) {
this.version = tempVersion;
this.setSilently(commit);
return;
}
tempVersion++;
}
};
}).call(this);