Skip to content

Instantly share code, notes, and snippets.

@johnpapa
Created June 18, 2012 03:44
Show Gist options
  • Save johnpapa/2946728 to your computer and use it in GitHub Desktop.
Save johnpapa/2946728 to your computer and use it in GitHub Desktop.
DirtyFlag
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