<?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; Events</title>
	<atom:link href="http://blog.justswell.org/tag/events/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>Even more Drag&amp;Drop coolness between your desktop and your browser!</title>
		<link>http://blog.justswell.org/even-more-dragdrop-coolness-with-event-delegation/</link>
		<comments>http://blog.justswell.org/even-more-dragdrop-coolness-with-event-delegation/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 15:41:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[DD]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Swell]]></category>
		<category><![CDATA[drag&drop]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://blog.justswell.org/?p=305</guid>
		<description><![CDATA[We are making more &#38; more progress with the native DnD API, and being native means fully taking advantage of Event Bubbling. In essence this means that if you want to make for example a datatable with a lot of rows without attaching events to them (remember the flyweight pattern?) you&#8217;ll only need to set [...]]]></description>
			<content:encoded><![CDATA[<p>We are making more &amp; more progress with the native DnD API, and being native means fully taking advantage of <strong>Event Bubbling</strong>.</p>
<p>In essence this means that if you want to make for example a datatable with a lot of rows without attaching events to them (remember the flyweight pattern?) you&#8217;ll only need to set your draggable element at a higher level in the hierarchy to get the DnD behavior applied on all children nodes (marked with the draggable attribute), that&#8217;s powerful and of course, reduces a lot the memory footprint of your web application.</p>
<p>A lot of components could benefit of this approach, Treeview, Datatable, Listview and so on.</p>
<p><strong>Another part of the example shows another level of interaction between the browser and desktop, and we are sure this one will get a lot of attention and excitement!</strong></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="581" height="363" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=6500920&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=c9ff23&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="581" height="363" src="http://vimeo.com/moogaloop.swf?clip_id=6500920&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=c9ff23&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://playground.justswell.org/drag-and-drop-event-delegation.html"><img class="aligncenter size-full wp-image-309" title="Swell drag&amp;drop in action" src="http://blog.justswell.org/wp-content/uploads/2009/09/dragging.png" alt="Swell drag&amp;drop in action" width="400" height="128" /></a>And the code <img src='http://blog.justswell.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> proxyElement <span style="color: #339933;">=</span> html.<span style="color: #660066;">div</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> html.<span style="color: #660066;">span</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'cls'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'dd-proxy-icon'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> html.<span style="color: #660066;">span</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Moo'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> draggableRows <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Swell.<span style="color: #660066;">Lib</span>.<span style="color: #660066;">DD</span>.<span style="color: #660066;">Draggable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bleh'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'delegate'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'proxy'</span>  <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'proxyOverride'</span>    <span style="color: #339933;">:</span> proxyElement<span style="color: #339933;">,</span>  
    <span style="color: #3366CC;">'dataCallback'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> _currentEl <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getDragEl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> _stdOut <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> _n <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> _l <span style="color: #339933;">=</span> _currentEl.<span style="color: #660066;">childNodes</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> _n <span style="color: #339933;">&lt;</span> _l<span style="color: #339933;">;</span> _n<span style="color: #339933;">++</span><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>_currentEl.<span style="color: #660066;">childNodes</span><span style="color: #009900;">&#91;</span>_n<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">nodeType</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> _text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>_currentEl.<span style="color: #660066;">childNodes</span><span style="color: #009900;">&#91;</span>_n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                _stdOut.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>_text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span>dt<span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/plain'</span><span style="color: #339933;">,</span> _stdOut.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> dropTarget <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Swell.<span style="color: #660066;">Lib</span>.<span style="color: #660066;">DD</span>.<span style="color: #660066;">Droppable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'moo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dropTarget.<span style="color: #660066;">ondrop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> draggable<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><span style="color: #339933;">!!</span>draggable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        draggable.<span style="color: #660066;">getDragEl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'moo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tBodies</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.justswell.org/even-more-dragdrop-coolness-with-event-delegation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>

