Created
March 30, 2012 19:40
-
-
Save tater9104/2254369 to your computer and use it in GitHub Desktop.
KnockoutJS IsDirty Extender
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
// Used to give an observable the ability to track whether or not its | |
// values have been changed. Adapted from a few blog posts: | |
// http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html | |
// http://schinckel.net/2012/01/14/knockoutjs-dirty-extender./ | |
// | |
// Requires: knockout.js, knockout.js mapping extension | |
ko.extenders.dirty = function(target) { | |
var cleanValue = ko.observable(ko.mapping.toJSON(target)); | |
target.isDirty = ko.computed(function(){ | |
return ko.mapping.toJSON(target) !== cleanValue(); | |
}); | |
return target; | |
}; | |
// When working with an array containing items that could be dirty, use | |
// you can quickly use the grep function in jQuery to extract those. | |
$.grep([ARRAY], function(n,i){ return [SOME CONDITION] }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment