Created
June 10, 2012 18:36
-
-
Save johnpapa/2906856 to your computer and use it in GitHub Desktop.
ko dirty
This file contains hidden or 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
| 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; |
This file contains hidden or 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) { | |
| '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; | |
| }; | |
| }); |
This file contains hidden or 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
| 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