Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

learning state design patterns in js #14

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
<!DOCTYPE html>
<html>
<head>
<title>So-and-so's Stateful Assignment</title>
</head>
<body>
YOUR PROJECT HERE
</body>
<title>Julia's Stateful Assignment</title>

<body>

<svg height="100" width="100">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>

</body>
</html>
</html>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function TrafficLight() {
this.state = new Stateful(this, "stop");
}

TrafficLight.States = {
stop: {
color: "red",
time: 8,

next: function() {
this.state.transition("go");
},

onEnterState: function() {
// Turn on traffic camera to see who crosses on a red light
},

onExitState: function() {
// Turn off traffic camera
}
},

go: {
color: "green",
time: 10,

next: function() {
this.state.transition("caution");
}
},

caution: {
color: "yellow",
time: 2,

next: function() {
this.state.transition("stop");
}
}
}

var light = new TrafficLight();
light.color //=> "red"
light.next()
light.color //=> "green"
light.next()
light.color //=> "yellow"
light.next()
light.color //=> "red"

//resource: https://gist.github.com/foca/3462441

</script>