Created
March 26, 2014 11:46
-
-
Save dazld/9781470 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
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