-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathknockout.dirtiable.js
More file actions
34 lines (29 loc) · 1.01 KB
/
knockout.dirtiable.js
File metadata and controls
34 lines (29 loc) · 1.01 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
/* A fleshed-out implementation of the dirtiable observables
* based on the the work done by Ryan Niemeyer.
* (http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html)
*/
ko.extenders.dirtiable = function(target) {
var _dirty = {};
_dirty.isDirty = ko.observable(false);
_dirty.initialState = null;
_dirty.tracker = ko.computed(function() {
if(!_dirty.isDirty())
ko.toJSON(target);
return(_dirty.isDirty());
});
//Track changes to observable
_dirty.tracker.subscribe(function() {
if(!_dirty.isDirty() && _dirty.initialState !== ko.toJSON(target))
_dirty.isDirty(true);
});
//Create the dirty flag on the observable
target.dirty = ko.computed(function() {
return(_dirty.isDirty());
});
//Allow users to reset the dirty flag
target.clean = function() {
_dirty.initialState = ko.toJSON(target);
_dirty.isDirty(false);
}
return target;
}