Skip to content

Instantly share code, notes, and snippets.

@geoffreyd
Created February 18, 2011 05:43
Show Gist options
  • Save geoffreyd/4cdb482c27eb896e632d to your computer and use it in GitHub Desktop.
Save geoffreyd/4cdb482c27eb896e632d to your computer and use it in GitHub Desktop.
StateChart Observer Code
SC.mixin(Ki.State.prototype,
/** SC.Object.prototype */ {
/**
Adds an observer that dispatches a "propertyChanged" (or `action` if set)
event to the object's statechart.
@param {String} key the property to observe
@param {SC.Object} target the object you want to observe the key on
@param {String} action Optional action function to call.
@returns {Function} the actual observer (need to remove later)
*/
addStatechartObserver: function(target, key, action) {
var state = this.statechart;
if (!action) {action = 'propertyChanged'}
var fun = function(target, key, value, revision) {
state.sendEvent(action, target, key, revision);
};
target.addObserver(key, target, fun);
return fun ;
},
/**
Adds an observer that dispatches a "propertyChanged" event to the object's
statechart.
@param {String} key the property to observe
@param {SC.Object} target the object you were observing the key on
@param {Function} the function object returned from addStatechartObserver
@returns {SC.Object} receiver
*/
removeStatechartObserver: function(target, key, fun) {
target.removeObserver(key, target, fun) ;
return this ;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment