backbone-fsm

Finite-State Machine for Backbone views and models.

Backbone-FSM

Finite-State Machine for Backbone views and models. Browser and Node.js compatible.

bower install backbone-fsm

Include lib/backbone-fsm.js into your page.

npm install backbone-fsm

FSM comes with mixin method. You have to extend this during initialization.

var Foo = Backbone.View.extend({
initializefunction () {
FSM.mixin(this);
}
});

You can define transitions and defaultState:

var Foo = Backbone.View.extend({
initializefunction () {
FSM.mixin(this);
},
 
defaultState: 'firstState',
 
transitions: {
'transitionName': {
from: 'firstState',
to: 'secondState'
}
},
 
onTransitionNamefunction (callback) {
callback();
}
});

To make a transition run transitionTo("secondState"). During transition:

  1. transition:start event is emited
  2. onTransitionName callback is called (if defined).
  3. transition:stop event is emited

Backbone-FSM live example: http://jsfiddle.net/fragphace/Zc6Gx/

For server-side tests:

npm test

Browser tests can be found in test/browser.html. Tested under:

  • IE 7+
  • FF 15+
  • Chrome 22+
  • Generate JS files - make coffee
  • Watch mode - make watch