Skip to content

Instantly share code, notes, and snippets.

@cjbell
Created July 10, 2012 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjbell/3082487 to your computer and use it in GitHub Desktop.
Save cjbell/3082487 to your computer and use it in GitHub Desktop.
State machine
(function(window, $){
var StateMachine = function(){};
StateMachine.fn = StateMachine.prototype;
$.extend(StateMachine.fn, {
subscribe: $.subscribe,
publish: $.publish
});
StateMachine.fn.add = function(controller){
this.subscribe("change", function(e, current){
if (controller == current)
controller.activate();
else
controller.deactivate();
});
controller.active = $.proxy(function(){
this.publish("change", controller);
}, this);
};
window.StateMachine = StateMachine;
})(window, window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment