Skip to content

Instantly share code, notes, and snippets.

@johnpapa
Created June 10, 2012 18:36
Show Gist options
  • Save johnpapa/2906856 to your computer and use it in GitHub Desktop.
Save johnpapa/2906856 to your computer and use it in GitHub Desktop.
ko dirty
var Attendance = function () {
var self = this;
self.datacontext = datacontext;
self.sessionId = ko.observable();
self.personId = ko.observable();
// id is string compound key {personId,sessionId} like "3,10"
self.id = ko.computed({
read: function () {
return attendanceMakeId(self.personId(), self.sessionId());
},
write: function (value) {
var idparts = value.split(",");
self.personId(parseInt(idparts[0]));
self.sessionId(parseInt(idparts[1]));
}
}),
self.rating = ko.observable();
self.text = ko.observable();
self.dirtyFlag = new ko.DirtyFlag(self);
return self;
};
var attendanceNullo = new Attendance()
.sessionId(0)
.personId(0)
.rating(0)
.text('');
attendanceNullo.dirtyFlag().reset();
attendanceNullo.isNullo = true;
define(['ko'], function(ko) {
'use strict';
ko.DirtyFlag = function (objectToTrack, isInitiallyDirty, hashFunction) {
hashFunction = hashFunction || ko.toJSON;
var
_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;
};
});
isDirty = ko.computed(function () {
if (session() && session().attendance && session().attendance()) {
return session().attendance().dirtyFlag().isDirty();
}
return false;
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment