-
-
Save svdmitrij/6e5a6a208ca3f39e302e 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
((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