Skip to content

Commit

Permalink
Add AMD support in dirty flag. #25
Browse files Browse the repository at this point in the history
  • Loading branch information
hfjallemark committed Nov 27, 2013
1 parent d09ef4b commit 87bb44a
Showing 1 changed file with 48 additions and 35 deletions.
83 changes: 48 additions & 35 deletions knockout.dirtyFlag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
//
// Knockout.DirtyFlag
//
// John Papa
// John Papa
// http://johnpapa.net
// http://twitter.com/@john_papa
//
// Depends on scripts:
// Knockout
// Knockout
//
// Notes:
// Special thanks to Steve Sanderson and Ryan Niemeyer for
// Special thanks to Steve Sanderson and Ryan Niemeyer for
// their influence and help.
//
// Usage:
// To Setup Tracking, add this tracker property to your viewModel
// 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?
// Did It Change?
// ===> viewModel.dirtyFlag().isDirty();
//
// Hook this into your view model functions (ex: load, save) ...
Expand All @@ -29,34 +29,47 @@
// Optionally, you can pass your own hashFunction for state tracking.
//
////////////////////////////////////////////////////////////////////////////////////////
;(function (ko) {
ko.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;

(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;
};
})(ko);
return result;
};
}));

0 comments on commit 87bb44a

Please sign in to comment.