Created
January 5, 2010 09:25
-
-
Save lacco/269267 to your computer and use it in GitHub Desktop.
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
/** | |
* 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