Created
June 18, 2012 03:44
-
-
Save johnpapa/2946728 to your computer and use it in GitHub Desktop.
DirtyFlag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['ko'], function(ko) { | |
// (function (ko) { | |
ko.DirtyFlag = function (objectToTrack, isInitiallyDirty, hashFunction) { | |
hashFunction = hashFunction || ko.toJSON; | |
var | |
_objectToTrack = objectToTrack, | |
_lastCleanState = ko.observable(hashFunction(_objectToTrack)), | |
_isInitiallyDirty = ko.observable(isInitiallyDirty), | |
result = function () { | |
var self = this; | |
self.isDirty = ko.computed(function () { | |
return _isInitiallyDirty() || hashFunction(_objectToTrack) !== _lastCleanState(); | |
}); | |
self.reset = function () { | |
_lastCleanState(hashFunction(_objectToTrack)); | |
_isInitiallyDirty(false); | |
}; | |
return self; | |
}; | |
return result; | |
}; | |
//}(ko)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment