<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brain Matters &#187; Javascript</title>
	<atom:link href="http://blog.agoragames.com/category/engineering/javascript-engineering/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agoragames.com</link>
	<description></description>
	<lastBuildDate>Thu, 29 Jul 2010 19:13:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript Continuations Make Callback Chains Easy</title>
		<link>http://blog.agoragames.com/2009/06/26/javascript-continuations-make-callback-chains-easy/</link>
		<comments>http://blog.agoragames.com/2009/06/26/javascript-continuations-make-callback-chains-easy/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 17:57:53 +0000</pubDate>
		<dc:creator>Ola Mork</dc:creator>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.agoragames.com/?p=295</guid>
		<description><![CDATA[The problem: I define hooks for callbacks in my javascript libraries. The functions for these hooks could be defined anywhere and tend to overwrite each other. When you want to write a callback you have to understand what the entire system is doing.
The solution: Instead of defining single functions for callbacks, maintain a list of [...]]]></description>
			<content:encoded><![CDATA[<p>The problem: I define hooks for callbacks in my javascript libraries. The functions for these hooks could be defined anywhere and tend to overwrite each other. When you want to write a callback you have to understand what the entire system is doing.</p>
<p>The solution: Instead of defining single functions for callbacks, maintain a list of them and then build a chain of continuations. Here is the completed code for managing our callbacks: <a href="http://gist.github.com/136525">http://gist.github.com/136525</a>. This expands on Jason&#8217;s continuations: <a href="http://lonelypinkelephants.com/continuing.html">http://lonelypinkelephants.com/continuing.html</a>.</p>
<p>The old way was to do something like this:</p>
<pre class="brush: js">
// file 1
Controllers.Hydra = {
  on_load : function() {
    do_whatever_is_defined_by_default()
  }
}
</pre>
<pre class="brush: js">
// file 2
Controllers.Hydra = {
  on_load : function() {
    do_something()
    do_this_other_thing()
    do_whatever_is_defined_by_default()
  }
}
</pre>
<p>And then in the code that actually cares:</p>
<pre class="brush: html">onload=&quot;Controllers.Hydra.on_load()&quot;</pre>
<p>This means the overloading method must know about what the default behavior is and it can only be overloaded once.</p>
<p>The new (awesome) way:</p>
<pre class="brush: js">
// file 1
Callbacks.add(Callbacks.Hydra.on_load, do_whatever_is_defined_by_default)
</pre>
<p>And:</p>
<pre class="brush: js">
// file 2
Callbacks.add(Callbacks.Hydra.on_load, do_something)
Callbacks.add(Callbacks.Hydra.on_load, do_this_other_thing)
</pre>
<p>And in the code that actually fires the callback:</p>
<pre class="brush: html">onload=&quot;Callbacks.start(Callbacks.Hydra.on_load)&quot;</pre>
<p>The callbacks are called in order.</p>
<p>What else can we do with this? One of the features I like about Rails is that if a callback or filter returns false the rest of the chain is halted. We&#8217;ve included that behavior too (This doesn&#8217;t necessarily affect what ever called start on the callback chain).</p>

<div class="sociable">

<ul>
	<li class="sociablefirst"><a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F06%2F26%2Fjavascript-continuations-make-callback-chains-easy%2F&amp;title=Javascript%20Continuations%20Make%20Callback%20Chains%20Easy" title="Reddit"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F06%2F26%2Fjavascript-continuations-make-callback-chains-easy%2F&amp;title=Javascript%20Continuations%20Make%20Callback%20Chains%20Easy&amp;bodytext=The%20problem%3A%20I%20define%20hooks%20for%20callbacks%20in%20my%20javascript%20libraries.%20The%20functions%20for%20these%20hooks%20could%20be%20defined%20anywhere%20and%20tend%20to%20overwrite%20each%20other.%20When%20you%20want%20to%20write%20a%20callback%20you%20have%20to%20understand%20what%20the%20entire%20system%20is%20doing.%0D" title="Digg"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F06%2F26%2Fjavascript-continuations-make-callback-chains-easy%2F&amp;title=Javascript%20Continuations%20Make%20Callback%20Chains%20Easy&amp;notes=The%20problem%3A%20I%20define%20hooks%20for%20callbacks%20in%20my%20javascript%20libraries.%20The%20functions%20for%20these%20hooks%20could%20be%20defined%20anywhere%20and%20tend%20to%20overwrite%20each%20other.%20When%20you%20want%20to%20write%20a%20callback%20you%20have%20to%20understand%20what%20the%20entire%20system%20is%20doing.%0D" title="del.icio.us"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fblog.agoragames.com%2F2009%2F06%2F26%2Fjavascript-continuations-make-callback-chains-easy%2F" title="Technorati"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.agoragames.com/2009/06/26/javascript-continuations-make-callback-chains-easy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->