<?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>Daniel Skinner: News and Articles on Web Development &#187; chain</title>
	<atom:link href="http://www.daniel-skinner.co.uk/tag/chain/feed" rel="self" type="application/rss+xml" />
	<link>http://www.daniel-skinner.co.uk</link>
	<description>Daniel Skinner&#039;s Blog: Web Development news and articles</description>
	<lastBuildDate>Wed, 18 Mar 2009 15:11:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Chaining with MooTools 1.2 &#8211; Tutorial</title>
		<link>http://www.daniel-skinner.co.uk/chaining-with-mootools-12-tutorial/31/01/2008</link>
		<comments>http://www.daniel-skinner.co.uk/chaining-with-mootools-12-tutorial/31/01/2008#comments</comments>
		<pubDate>Thu, 31 Jan 2008 22:30:11 +0000</pubDate>
		<dc:creator>Daniel Skinner</dc:creator>
				<category><![CDATA[Guides]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chain]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.daniel-skinner.co.uk/chaining-with-mootools-12-tutorial/31/01/2008</guid>
		<description><![CDATA[A short tutorial on how to use Chaining in MooTools.]]></description>
			<content:encoded><![CDATA[<p>This guide will show how powerful the <strong>MooTools Chain class</strong> is. In MooTools chaining facilitates the execution of a stack of functions sequentially and is extremely powerful. I have only tested this in <strong>MooTools v1.2 beta 2</strong>.</p>
<p>I will be posting more of these short guides on using MooTools in the upcoming weeks. Each will focus on a small problem and solve it with MooTools with the aim of being a useful way to learn the framework.</p>
<p><span id="more-15"></span></p>
<h3>Chaining you say!</h3>
<p><strong>Chaining</strong> is very powerful and whilst it is similar to <code>Effect.Queue</code> in Script.aculo.us, it can do much more! The aim of this brief tutorial is to show you how to use <strong>MooTools Chaining</strong> in the context of queueing effects coupled with other arbitrary actions. However, you will see that you can easily apply Chaining to do much more than that.</p>
<p>From the MooTools 1.2 Docs:</p>
<blockquote><p>A Utility Class which executes functions one after another, with each function firing after completion of the previous. Its methods can be implemented with <code><a href="http://docs12b.mootools.net/Class/Class/#Class:implement" title="MooTools Documentation: Class:implement" rel="external nofollow">Class:implement</a></code> into any <code><a href="http://docs12b.mootools.net/Class/Class" title="MooTools Documentation: Class" rel="external nofollow">Class</a></code>, and it is currently implemented in <code>Fx</code> and <code>Request</code>. In <code>Fx</code>, for example, it is used to create custom, complex animations.</p></blockquote>
<p><code>Chain</code> can be used as a stand-alone class but becomes much more powerful if you implement it into classes of your own.</p>
<p>In this tutorial I will be creating a class which implements <code>Chain</code>. The <code>Chain</code> class belongs to the <code>Class.Extras</code> component so you will need to make sure that you select <code>Class.Extras</code> and all its dependencies when downloading MooTools.</p>
<h3>Implementing Chain</h3>
<p>First of all we need to implement the methods and properties of the <code>Chain</code> class into our own class:</p>
<pre lang="javascript">var ChainExample = new Class({

  Implements: [Chain]

});</pre>
<p>Our class now has the following methods available to it:</p>
<ul>
<li><strong><code>Chain::chain</code></strong> &#8211; Pass any number of functions to add them to the bottom of the call stack.</li>
<li><strong><code>Chain::callChain</code></strong> &#8211; Pops a function off the top of the call stack and executes it.</li>
<li><strong><code>Chain::clearChain</code></strong> &#8211; Removes all functions from the call stack without executing any.</li>
</ul>
<h3>Using Chain to Execute Actions in Order</h3>
<p>Now we have implemented <code>Chain</code> into our <code>ChainExample</code> class we are ready to make use of the functionality it provides.</p>
<p>The <code>ChainExample</code> class will add events to three button elements. When a button element is clicked a corresponding panel will appear. Before this happens all other panels will be faded out. Chaining is used to make sure that these events happen in the order we want.</p>
<p>The full code for <code>ChainExample</code> is:</p>
<pre lang="javascript">
/**

 * Add events to buttons. Clicking a button will hide all panels before showing the panel corresponding to that button

 */

var ChainExample = new Class({

    Implements: [Chain],

    /**

     * Define the element ID of the button and the element ID of the corresponding panel

     */

    actions: new Hash({

        'button-one': 'panel-one',

        'button-two': 'panel-two',

        'button-three': 'panel-three'

    }),

    /**

     * An Array to store an effect instance for each panel

     */

    effects: [],

    initialize: function()

    {

        /**

         * Add an onclick event to each button. Clicking a button calls the showPanel method

         */

        this.actions.getKeys().each(function(buttonId) {

            $(buttonId).addEvent('click', this.showPanel.bindWithEvent(this));

        },this);

        /**

         * Create an Fx object for each panel

         */

         this.actions.getValues().each(function(panelId) {

            this.effects[panelId] = new Fx.Tween($(panelId), 'opacity', { duration: 'short', onComplete: function() { this.callChain();}.bind(this)});

         }, this);         /**

          * Initialize by hiding all panels, note the call to callChain to cause stuff to happen

          */

        this.hideAll();

        this.callChain();

    },

    /**

     * Add the a actions required to hide all panels to the Chain call stack

     */

    hideAll: function()

    {

        /**

         * loop each panel and Chain: 1. fade the panel, 2. set the display property to "none" after the effect has finished.

         *

         * Note that this function does not actually cause anything to happen, it simply adds actions to the Chain

         */

        this.actions.getValues().each(function(panelId) {

            this.chain(

                function() { this.effects[panelId].start(0); },

                function() { $(panelId).setStyles({'display': 'none'}); this.callChain(); }

            );

        },this);

    },

    /**

     * Handle a button click by fading and hiding all open panels and then appearing the corresponding panel

     */

    showPanel: function(event)

    {

        this.hideAll();

        var panel = this.actions.get(event.target.get('id'));

        this.chain(

            function() { $(panel).setStyles({'display': 'block', 'opacity': '0'}); this.callChain(); },

            function() { this.effects[panel].start(1); }

        );

        this.callChain(); //this call starts the chain. Since each function in the call also makes a call to callChain the entire stack will be executed

    }

});

window.addEvent('domready',

    function()

    {

        var myChain = new ChainExample();

    }

);</pre>
<p>I will only go through the code relevant to chaining in detail. Lets have a look at each important part:</p>
<h4><code>ChainExample::initialize</code></h4>
<p>In the constructor we bind behaviours to the buttons and create <code>Fx</code> objects for the panels. Notice the custom <code>onComplete</code> callback provided for each <code>Fx</code> instance. This tells the internal chain stack to pop the next function and run it right after the effect has finished. We are now able to execute any function as soon as the effect completes.</p>
<h4><code>ChainExample::hideAll</code></h4>
<p>Look at this method carefully. A call to <code>ChainExample::hideAll()</code> does not actually hide the panels. The method adds to the chain stack a set of functions that will fade and hide each panel in order. To get the panels to actually hide we must execute the chain stack using <code>this.callChain()</code>.</p>
<p>Notice how the second function passed to <code>this.chain</code> invokes <code>this.callChain()</code>. The function is telling the chain stack to continue onto the next function by itself.</p>
<p>Another important thing to note is that there is no need to bind the function passed to <code>this.chain</code> as this is dealt with internally.</p>
<h4><code>ChainExample::showPanel</code></h4>
<p>This is the event handling function that will show a panel.</p>
<p>First off a call to <code>ChainExample::hideAll()</code> is made. Remember that this method doesn&#8217;t cause anything to happen immediately. At this point we have added all the steps needed to hide all the panels to the chain stack. We then proceed to add steps which will show the correct panel.</p>
<p>Once the entire chain stack is set up <code>this.callChain()</code> is executed and the entire stack will be called because each step makes its own call to <code>this.callChain()</code> once it has finished.</p>
<p>That&#8217;s the basics of <code>Chain</code>. We have created a class which allows us to combine any number of effects and arbitrary functions and ensure that the are executed in the order we want. Hence the basic idea of <code>Chain</code> is to create a stack of functions and execute them in the order you please whenever you please.</p>
<h3>Downloadable Demo</h3>
<p>I have put together a quick demo so that you can see the code in action. Download the demo <a href="http://www.destiny-denied.co.uk/files/moo-chain.zip">here</a>.</p>
<h3>Further Considerations</h3>
<p>In my example, every function in the chain stack makes a call to <code>this.callChain()</code> once is has completed.  This means that as soon as the first function is executed (by manually invoking <code>this.callChain()</code>) the entire stack will run from start to finish automatically. <code>Chain</code> can be used in a different fashion where <code>Chain::callChain()</code> is always invoked externally. As a crude modification to my example, every click on a button could invoke <code>this.callChain()</code>. Thus each button click takes one further step to completion of the actions.</p>
<p>If you need to add timings and delays consider using <code><a href="http://docs12b.mootools.net/Native/Function#Function:delay" title="MooTools Documentation: Function:Delay" rel="external nofollow">Function:delay</a></code> to delay the invocation of <code>Chain::callChain()</code>.</p>
<p>It is also possible to use multiple chains in the same class for even more complex behaviour. This would involve using separate instances of the Chain class directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daniel-skinner.co.uk/chaining-with-mootools-12-tutorial/31/01/2008/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>
