<?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>SwellJS - another big fish in the sea &#187; Custom event</title>
	<atom:link href="http://blog.justswell.org/tag/custom-event/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justswell.org</link>
	<description>well-minded javascript library</description>
	<lastBuildDate>Fri, 20 Aug 2010 20:48:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Painless publish/subscribe system with swell&#8217;s custom events</title>
		<link>http://blog.justswell.org/painless-publishsubscribe-system-with-swells-custom-events/</link>
		<comments>http://blog.justswell.org/painless-publishsubscribe-system-with-swells-custom-events/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 16:21:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Swell]]></category>
		<category><![CDATA[Custom event]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Publish/subscribe pattern]]></category>

		<guid isPermaLink="false">http://blog.justswell.org/?p=58</guid>
		<description><![CDATA[Let me start with wikipedia&#8217;s own publish/subscribe pattern definition : Publish/subscribe (or pub/sub) is an asynchronous messaging paradigm where senders (publishers) of messages are not programmed to send their messages to specific receivers (subscribers). This decoupling of publishers and subscribers can allow for greater scalability&#8230; We absolutely wanted to have this pattern implemented into Swell [...]]]></description>
			<content:encoded><![CDATA[<p>Let me start with wikipedia&#8217;s own publish/subscribe pattern definition : Publish/subscribe (or pub/sub) is an asynchronous messaging paradigm where senders (publishers) of messages are not programmed to send their messages to specific receivers (subscribers). This decoupling of publishers and subscribers can allow for greater scalability&#8230;  We absolutely wanted to have this pattern implemented into Swell as we are fervent supporters of the SoC. The goal was to be able to hook the Custom event model to potentially any class as well as using custom events outside of a class context.</p>
<h3>Custom event model</h3>
<p>By inheriting the CustomEventModel class built-into swell, the children class is augmented with new methods and collections brought from the model.  Example :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    Swell.<span style="color: #660066;">Core</span>.<span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span>        <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Hashtable'</span><span style="color: #339933;">,</span>
        <span style="color: #003366; font-weight: bold;">namespace</span>   <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Swell.Core'</span><span style="color: #339933;">,</span>
        inherits    <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">Enumerable</span><span style="color: #339933;">,</span> Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">CustomEventModel</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        ...</pre></div></div>

<p>Swell.Core.Hashtable class now have those methods :</p>
<ul>
<li><span style="color: #008000;"><strong>createEvent</strong></span><em><span style="color: #008000;"> </span><span style="color: #808080;">(creates a custom event that is bound to the class ex : onHashUpdated)</span></em></li>
<li><span style="color: #008000;"><strong>fireEvent</strong></span><em><span style="color: #008000;"><strong> </strong></span><span style="color: #808080;">(fires desired event and will notice all the subscribers aka execute callback functions)</span></em></li>
<li><span style="color: #008000;"><strong>subscribe</strong> </span><em><span style="color: #808080;">(subscribe to a custom event of the class from/outside the class)</span></em></li>
<li><span style="color: #008000;"><strong>unsubscribe</strong></span></li>
<li><span style="color: #008000;"><strong>getEvents</strong></span></li>
</ul>
<p>In the hashtable code, we use custom events to update a static property containing the length.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _initEvents <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/**
      * Initialization of the event
      * @event onChange fires when an item is added/updated in the hashtable
    */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onHashUpdated'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onHashUpdated'</span><span style="color: #339933;">,</span> _updateLength<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//_updateLength is a private function of the class</span>
<span style="color: #009900;">&#125;</span>
...
<span style="color: #006600; font-style: italic;">/**
 * Now triggering the event
*/</span>
set <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _hash<span style="color: #009900;">&#91;</span>Swell.<span style="color: #660066;">uniqueId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> key<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        _hash<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onHashUpdated'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
...</pre></div></div>

<h3>Standalone custom events</h3>
<p>This is the simplest way of using the custom event system.<br />
In the example below we have created a basic object called button that makes use of custom events.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> button <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setValue</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        value <span style="color: #339933;">=</span> val<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onValueChange</span>.<span style="color: #660066;">fire</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onValueChange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">CustomEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onValueChange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button value has changed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> btn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> button<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
btn.<span style="color: #660066;">onValueChange</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
btn.<span style="color: #660066;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'toto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And the firebug DOM :</p>
<div id="attachment_70" class="wp-caption aligncenter" style="width: 580px"><img class="size-full wp-image-70" title="Firebug DOM" src="http://blog.justswell.org/wp-content/uploads/2009/03/btn.png" alt="Firebug DOM" width="570" height="237" /><p class="wp-caption-text">Firebug DOM</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.justswell.org/painless-publishsubscribe-system-with-swells-custom-events/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

