Skip to content

Instantly share code, notes, and snippets.

@dazld
Created March 26, 2014 11:46
Show Gist options
  • Save dazld/9781470 to your computer and use it in GitHub Desktop.
Save dazld/9781470 to your computer and use it in GitHub Desktop.
var Backbone = require('backbone');
var FSM = Backbone.Model.extend({
states: {
"full": {
: []
},
"guessed": {
transitions: {}
},
"partial": {
transitions: {}
},
"reset": {
transitions: {}
}
},
transition: function(options){
this.emit('transition', {
from: options.from,
to: options.to,
data: options.data
});
}
});
var FSMView = Backbone.View.extend({
initialize: function (options) {
this.fsm = options.fsm;
this.listenTo(this.fsm, 'transition', this.onTransition);
// views have same names as states in model?
this.viewForFull = FullView
this.viewForPartial = PartialView
this.viewForGuess = GuessView
},
onTransition: function(data){
var old = data.from;
var to = data.to;
// this.toggle(this.currentView, this.views[to]);
this.detach(this.currentView);
this.attach(this.views[to]);
this.emit('transitioned');
}
});
module.exports = FSM;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment