<?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>techno.blog(&#34;Dion&#34;) &#187; events</title>
	<atom:link href="http://almaer.com/blog/tag/events/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Fri, 25 May 2012 16:56:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enjoying the Observer pattern with custom events</title>
		<link>http://almaer.com/blog/enjoying-the-observer-pattern-with-custom-events</link>
		<comments>http://almaer.com/blog/enjoying-the-observer-pattern-with-custom-events#comments</comments>
		<pubDate>Wed, 06 Aug 2008 15:51:24 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/enjoying-the-observer-pattern-with-custom-events</guid>
		<description><![CDATA[I created an introductory example discussing custom events as an implementation of the Observer pattern.

The example dynamically adds functionality based on checkboxes to simulate events that could change processing and uses the fire() and observe() methods from Prototype.
I strap on behaviour to a list of names. When you click on the name, something can happen. [...]]]></description>
			<content:encoded><![CDATA[<p>I created an <a href="http://google-ajax-examples.googlecode.com/svn/trunk/customevents/index.html">introductory example</a> discussing custom events as an implementation of the Observer pattern.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/6no9buuP1OI&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/6no9buuP1OI&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>The example dynamically adds functionality based on checkboxes to simulate events that could change processing and uses the <code>fire()</code> and <code>observe()</code> methods from Prototype.</p>
<p>I strap on behaviour to a list of names. When you click on the name, something can happen. To do this I could have done the following:</p>
<pre language="javascript">
$$('ul#leftchoices li').each(function(el) {
    el.observe('click', function(e) {
        if ($('colorchange').checked) {
           changeColor();
        }

        if ($('contentchange').checked) {
           changeContent(el.id);
        }
    });
});
</pre>
<p>In the click event itself, I do various checks and kick off behaviour. That can be fine for small actions, but what if you want to do more? This is when I prefer to abstract out the action and just fire an event:</p>
<pre language="javascript">
$$('ul#leftchoices li').each(function(el) {
    el.observe('click', function(e) {
        el.fire('selected:choice');
    });
});
</pre>
<p>At this point, this bit of code becomes dumb. It doesn&#8217;t know what to do when you select the item, and it just hopes that somewhere, someone is listening. That is where the observers come in, such as this one that changes the main content based on the selected name:</p>
<pre language="javascript">
$('contentchange').onchange = function(e) {
    if (e.target.checked) {
        document.observe('selected:choice', changeContent);
    } else {
        document.stopObserving('selected:choice', changeContent);
    }
}
</pre>
<p>When someone clicks on the checkbox, this method is fired and an observer is either added, or taken away.</p>
<p>Once you start building applications with this in mind, you may find a bit of a sea change. You start to think about the various events as a public API that you can easily expose to observers. Gone is the simple ability to look at one method and see what is happening, but the loose coupling gives you the ability to easily layer in your architecture. Based on some settings, behaviour can be dynamically added. You could even expose these events in a way that makes it easier for Greasemonkey hackers to come in and work with your application. All in all, a win-win for anything more than a simple example.</p>
<p>Although this example uses Prototype, you could do the same with the other top class JavaScript libraries. In fact, if someone wants to port this example to Dojo, jQuery, Mootools, YUI, or anything else, send me the files and I will put them up so we can all compare how custom events are done in the various toolkits.</p>
<p><em>UPDATE: We now have <a href="http://google-ajax-examples.googlecode.com/svn/trunk/customevents/prototype.html">Prototype</a>, <a href="http://google-ajax-examples.googlecode.com/svn/trunk/customevents/jquery.html">jQuery</a>, <a href="http://google-ajax-examples.googlecode.com/svn/trunk/customevents/dommassistant.html">DOMAssistant</a>, and <a href="http://google-ajax-examples.googlecode.com/svn/trunk/customevents/appcelerator.html">Appcelerator</a> versions. Thanks Malte!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/enjoying-the-observer-pattern-with-custom-events/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Gears Future APIs: Messaging API</title>
		<link>http://almaer.com/blog/gears-future-apis-messaging-api</link>
		<comments>http://almaer.com/blog/gears-future-apis-messaging-api#comments</comments>
		<pubDate>Thu, 27 Dec 2007 15:47:38 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Gears]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[apis]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[messaging]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/gears-future-apis-messaging-api</guid>
		<description><![CDATA[Once you start delving into the WorkerPool API, you quickly see how a common pattern would be using it as a messaging system. As it stands, it looks like an Open Web version of Erlang processes.
Scott Hess wrote up his thoughts on a more formal Messaging API based on WorkerPool:

Gears WorkerPool has two pieces, the [...]]]></description>
			<content:encoded><![CDATA[<p>Once you start delving into the <a href="http://code.google.com/apis/gears/api_workerpool.html">WorkerPool API</a>, you quickly see how a common pattern would be using it as a messaging system. As it stands, it looks like an Open Web version of Erlang processes.</p>
<p>Scott Hess wrote up his thoughts on a more formal <a href="http://code.google.com/p/google-gears/wiki/MessagingAPI">Messaging API</a> based on WorkerPool:</p>
<blockquote><p>
Gears WorkerPool has two pieces, the part about running a bit of JS asynchronously, and the part about trading messages with that JS. This API may be composable from more basic bits. The messaging bit could be used in other contexts, such as implementing something like <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-crossDocumentMessages.html">WhatWG&#8217;s postMessage()</a>.
</p></blockquote>
<p><b>Aside: What is WhatWG&#8217;s postMessage?</b></p>
<p><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-crossDocumentMessages.html">postMessage</a> is &#8220;a messaging system that allows documents to communicate with each other regardless of their source domain, in a way designed to not enable cross-site scripting attacks.&#8221;</p>
<p>Here is an example:</p>
<blockquote><p>
For example, if document A contains an <code><a href="section-embedded0.html#object">object</a></code> element that contains document B, and<br />
    script in document A calls <code title="dom-window-postMessage"><a href="#postmessage">postMessage()</a></code> on document B, then a<br />
    message event will be fired on that element, marked as originating from<br />
    document A. The script in document A might look like:</p>
<pre>var o = document.getElementsByTagName('object')[0];
o.<span title="dom-object-contentWindow">contentWindow</span>.<a href="#postmessage" title="dom-window-postMessage">postMessage</a>('Hello world');
</pre>
<p>To register an event handler for incoming events, the script would use<br />
    <code title="">addEventListener()</code> (or similar mechanisms). For<br />
    example, the script in document B might look like:</p>
<pre>document.addEventListener('message', receiver, false);
function receiver(e) {
  if (e.domain == 'example.com') {
    if (e.data == 'Hello world') {
      e.source.postMessage('Hello');
    } else {
      alert(e.data);
    }
  }
}</pre>
<p>This script first checks the domain is the expected domain, and then<br />
    looks at the message, which it either displays to the user, or responds<br />
    to by sending a message back to the document which sent the message in<br />
    the first place.</p>
</blockquote>
<p><b>Back to the Gears Messaging API</b></p>
<p>Scott gives an example of the messaging API, starting with an end point:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> port <span style="color: #339933;">=</span> google.<span style="color: #660066;">gears</span>.<span style="color: #660066;">factory</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beta.messageport'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>port<span style="color: #339933;">,</span> msg<span style="color: #339933;">,</span> sender<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;">&quot;message: &quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// Omit for anonymous listener.</span></pre></div></div>

<p>and having a way to send it a message:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> port <span style="color: #339933;">=</span> google.<span style="color: #660066;">gears</span>.<span style="color: #660066;">factory</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beta.messageport'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">sendMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello there&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To enable cross domain, you can <code>post.open(name, domain)</code>, and on the other side, you have to allow it via something like <code>port.allowCrossOrigin(["www.good.com", "www.angelic.com"]);</code>.</p>
<p>I am excited about a messaging API, as I think that it fits into the way in which we are developing new Web applications. Having an asynchronous queue that allows me to replay work (e.g. offline), and work nicely with Comet based interactions, would be great. We can reuse all that we have learned from other event based systems, and <a href="http://www.enterpriseintegrationpatterns.com/ramblings.html">Gregor</a> can rename his book and be happy!</p>
<p><b>Other Future APIs</b></p>
<ul>
<li><a href="http://almaer.com/blog/gears-future-apis-location-api">Location API</a></li>
<li><a href="http://almaer.com/blog/gears-future-apis-desktop-shortcut-api">Desktop Shortcut API</a></li>
<li><a href="http://almaer.com/blog/gears-future-apis-image-manipulation-api">Image Manipulation API</a></li>
<li><a href="http://almaer.com/blog/google-gears-upgrading-from-a-1950s-chevy-in-cuba">Introduction to the series</a></li>
</ul>
<p><em>Disclaimer: This is early days, and who knows what the final API will look like, or if it will even make it. Do you have ideas for cool Gears that make the Web better? <a href="http://groups.google.com/group/google-gears/">Let us know!</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/gears-future-apis-messaging-api/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

