Skip to content

Instantly share code, notes, and snippets.

@svdmitrij
Last active January 22, 2016 18:20
Show Gist options
  • Save svdmitrij/6e5a6a208ca3f39e302e to your computer and use it in GitHub Desktop.
Save svdmitrij/6e5a6a208ca3f39e302e to your computer and use it in GitHub Desktop.
((window) ->
$ = window.$
startApp = ->
StateMachine = window.StateMachine
$stateText = $("span.state")
fsm = StateMachine.create(
initial: "hungry"
events: [
name: "eat"
from: ["hungry", "hungrysick"]
to: "satisfied"
,
name: "eat"
from: "satisfied"
to: "full"
,
name: "eat"
from: "full"
to: "sick"
,
name: "eat"
from: "sick"
to: "dead"
,
name: "rest"
from: "hungrysick"
to: "dead"
,
name: "rest"
from: ["satisfied", "full", "sick"]
to: "hungry"
,
name: "rest"
from: "hungry"
to: "hungrysick"
,
name: "clear"
from: "*"
to: "hungry"
]
callbacks:
onenterstate: ->
$("#state span").html @current
onhungry: ->
$stateText.css color: "red"
onleavehungry: ->
$stateText.css color: "black"
onsatisfied: ->
$stateText.css color: "green"
onleavesatisfied: ->
$stateText.css color: "black"
)
$(".buttons input").on "click", (e) ->
clickedElementName = e.target.name
try
fsm[clickedElementName]()
catch err
console.log err
$ ->
startApp()
) this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment