-
Notifications
You must be signed in to change notification settings - Fork 49
/
knockout.dirtyFlag.js
75 lines (67 loc) · 2.4 KB
/
knockout.dirtyFlag.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
// By: Hans Fjällemark and John Papa
// https://github.com/CodeSeven/KoLite
//
// Knockout.DirtyFlag
//
// John Papa
// http://johnpapa.net
// http://twitter.com/@john_papa
//
// Depends on scripts:
// Knockout
//
// Notes:
// Special thanks to Steve Sanderson and Ryan Niemeyer for
// their influence and help.
//
// Usage:
// To Setup Tracking, add this tracker property to your viewModel
// ===> viewModel.dirtyFlag = new ko.DirtyFlag(viewModel.model);
//
// Hook these into your view ...
// Did It Change?
// ===> viewModel.dirtyFlag().isDirty();
//
// Hook this into your view model functions (ex: load, save) ...
// Resync Changes
// ===> viewModel.dirtyFlag().reset();
//
// Optionally, you can pass your own hashFunction for state tracking.
//
////////////////////////////////////////////////////////////////////////////////////////
(function (factory) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
factory(require("knockout"), exports);
} else if (typeof define === "function" && define["amd"]) {
define(["knockout", "exports"], factory);
} else {
factory(ko, ko);
}
}(function (ko, exports) {
if (typeof (ko) === undefined) {
throw 'Knockout is required, please ensure it is loaded before loading the dirty flag plug-in';
}
exports.DirtyFlag = function (objectToTrack, isInitiallyDirty, hashFunction) {
hashFunction = hashFunction || ko.toJSON;
var
self = this,
_objectToTrack = objectToTrack,
_lastCleanState = ko.observable(hashFunction(_objectToTrack)),
_isInitiallyDirty = ko.observable(isInitiallyDirty),
result = function () {
self.forceDirty = function ()
{
_isInitiallyDirty(true);
};
self.isDirty = ko.computed(function () {
return _isInitiallyDirty() || hashFunction(_objectToTrack) !== _lastCleanState();
});
self.reset = function () {
_lastCleanState(hashFunction(_objectToTrack));
_isInitiallyDirty(false);
};
return self;
};
return result;
};
}));