Skip to content

Instantly share code, notes, and snippets.

@tater9104
Created March 30, 2012 19:40
Show Gist options
  • Save tater9104/2254369 to your computer and use it in GitHub Desktop.
Save tater9104/2254369 to your computer and use it in GitHub Desktop.
KnockoutJS IsDirty Extender
// 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