Skip to content

Instantly share code, notes, and snippets.

@lacco
Created January 5, 2010 09:25
Show Gist options
  • Save lacco/269267 to your computer and use it in GitHub Desktop.
Save lacco/269267 to your computer and use it in GitHub Desktop.
/**
* Registering callbacks on property setters.
*/
for(var prop in object){
// Ignore functions
if(typeof object[prop] !== 'function'){
(function(obj, attr) {
// If object does not already have a getter
if (!object.__lookupGetter__(prop)) {
// This variable will hold the value of the property
var value = obj[attr];
// Define the setter which will invoke the callbacks.
obj.__defineSetter__(attr, function(v) {
// Call all registered callbacks
ObjectObserver._getCallbacks(obj).each(function(method){
method(obj, attr, v);
});
value = v;
});
// Define corresponding getter
obj.__defineGetter__(attr, function() {
return value;
});
}
})(object, prop);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment