-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasync.html
More file actions
179 lines (172 loc) · 6.08 KB
/
async.html
File metadata and controls
179 lines (172 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src='http://use.typekit.com/aow5drr.js' type='text/javascript'></script>
<script type='text/javascript'>
try{ Typekit.load(); } catch(e) {}
</script>
<meta charset='utf-8' />
<meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible' />
<meta content='The Miso Project' name='description' />
<meta content='The Miso Project' name='name' />
<meta content='width=device-width' name='viewport' />
<title>The Miso Project :: Storyboard :: Tutorials</title>
<link href='/stylesheets/screen.css' rel='stylesheet' />
<link href='/stylesheets/codemirror.css' rel='stylesheet' />
</head>
<body>
<nav>
<span class='miso'>
<a href='/'>
← Miso Home
</a>
</span>
<span>
<a href='/storyboard'>
Home
</a>
</span>
<span>
<a href='/storyboard/tutorials.html'>
Tutorials
</a>
</span>
<span>
<a href='/storyboard/api.html'>
API Doc
</a>
</span>
<span>
<a href='http://www.github.com/misoproject/storyboard'>
Code (Github)
</a>
</span>
</nav>
<article class='storyboard'>
<section class='tutorial'>
<h2>Asynchronous Scenes</h2>
<p>
More often than not, a lot of your code will be asynchronous by nature. As you know
in JavaScript, this can mean your function actually exists before the asynchronous
code is done running! If you are trying to chain behavior, this can be a problem because
things will get out of sync pretty fast.
</p>
<p>
Storyboard scenes can declare themselves as being asynchronous. To do so you need to:
<ul>
<li>
Create a variable that saves the return of the function call <code>this.async()</code>.
This marks the function as async and as a result doesn't resolve the deferred it
normally would.
</li>
<li>
When the async work is done, call that saved function to signal that the work is
indeed done.
</li>
</ul>
</p>
<div class="codeblock"><textarea name="codehelper" class="code" mode="" runnable="true" showConsole="true" buttons="run,reset,clear" autorun="" callbacks-clear="" callbacks-reset="" callbacks-run="">
var story = new Miso.Storyboard({
initial : 'loading',
scenes : {
// do nothing here... just kicking things off.
loading : {},
a : {
enter : function() {
// declare this function is asynchronous!
// Calling this.async returns a _function_
// you should then call when you're done with all
// your work.
var done = this.async();
log("Going to start some time consuming work...");
// do your async work here.... for example
// in a setTimeout!
setTimeout(function() {
// do something.....
log("Done with my time consuming work...!");
// then call the done function when you're done!
done();
}, 1500);
}
},
b : {
enter : function() {
log("And now we've transitioned to scene b...");
}
}
}
});
story.start().then(function() {
story.to('a').then(function() {
story.to('b');
});
});</textarea><div class="helptext">You can edit the code in this block and rerun it.</div></div>
<a href='/storyboard/tutorials/quickstart' style='float:left;'>
<h2>
« Quickstart Guide
</h2>
</a>
<a href='/storyboard/tutorials/remotedata' style='text-align:right;'>
<h2>
Remote Data »
</h2>
</a>
</section>
</article>
<footer>
<div class='container'>
<section>
<h2>
A joint project between
</h2>
<div class='content'>
<a href='http://www.guardian.co.uk/profile/guardian-interactive-department'>
<img alt="Guardian" src="/images/guardian.png"/>
</a>
<a href='http://www.bocoup.com'>
<img alt="Bocoup horizontal 614" src="/images/bocoup-horizontal-614.gif"/>
</a>
</div>
</section>
<section>
<h2>
Supported by
</h2>
<div class='content'>
<a href='http://www.guardian.co.uk/development'>
<img alt="Global dev" src="/images/global-dev.png"/>
</a>
<a href='http://www.gatesfoundation.org/'>
<img alt="Gates" src="/images/gates.png"/>
</a>
</div>
</section>
</div>
</footer>
<script src='/js/jquery.min.js'></script>
<script src='/js/nav.js'></script>
<script src='/js/miso.storyboard.deps.js'></script>
<script src='/js/bootstrap/transition.js'></script>
<script src='/js/bootstrap/collapse.js'></script>
<script charset='utf-8' src='/js/ace-min/ace.js' type='text/javascript'></script>
<script src='/js/acify.js'></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30531498-1']);
_gaq.push(['_setDomainName', 'misoproject.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>