<?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>JavaScript Infected &#187; jQuery</title>
	<atom:link href="http://www.julianwong.net/blog/category/javascript/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.julianwong.net/blog</link>
	<description>Do everything with javascript</description>
	<lastBuildDate>Sat, 13 Aug 2011 07:32:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Using custom event on jQuery</title>
		<link>http://www.julianwong.net/blog/2009/09/using-custom-event-on-jquery/</link>
		<comments>http://www.julianwong.net/blog/2009/09/using-custom-event-on-jquery/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 08:04:33 +0000</pubDate>
		<dc:creator>julian</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://julianwong.net/blog/?p=155</guid>
		<description><![CDATA[When doing Javascript, it is common to handle different DOM event like click, mouseover, keydown&#8230; This is cool that with the DOM triggering those events, we can create custom handling / behaviors on it. Say, when user click on the button, a edit panel fade in. But this is not good enough when we have [...]]]></description>
			<content:encoded><![CDATA[<p>When doing Javascript, it is common to handle different DOM event like click, mouseover, keydown&#8230; This is cool that with the DOM triggering those events, we can create custom handling / behaviors on it. Say, when user click on the button, a edit panel fade in. But this is not good enough when we have to deal with a complex / feature rich interface. For instance, if a feature require a series of action on different components, how can we do it in a proper way? Here is custom event come into play.</p>
<p>There are different ways of making custom event to works, following will show:<br />
1. Using jQuery.bind and jQuery.trigger<br />
2. Using the jQuery plugin jQuery.mhub<br />
<span id="more-155"></span></p>
<h3>Using jQuery.bind and jQuery.trigger</h3>
<p>Have searched for how to have custom event on jQuery, found that <a href="http://yoan.dosimple.ch/blog/2008/04/30/">yoan&#8217;s Introduction to jQuery’s custom event</a> had some good insight. He had achieved it by using the bind and trigger function in jQuery. I have referenced his blog and the API from jQuery and created a rather primitive sample.</p>
<pre>var count = 0;
var elLog = document.getElementById("log");

// trigger the ticker event into body element
setInterval(function() {
    <strong>$("body").trigger("ticker",{name:"my name is foo",count:count++});</strong>
},3000);

<strong>$("body").bind("ticker",gFunc);</strong>

function gFunc(evt,msg) {
    elLog.innerHTML += "gFunc triggered&lt;br/&gt;&lt;br/&gt;";
}

var myApp = {
    name:"app1",

    init:function() {
        var that = this;
        // bind the _handlerTicker function to the ticker event of body element
        <strong>$("body").bind("ticker",function() {that._handlerTicker.apply(that,arguments)});</strong>
    },

    _handlerTicker:function(evt,msg) {
        elLog.innerHTML += this.name + "&lt;br/&gt;";
        elLog.innerHTML += "event name: " + evt.type + "&lt;br/&gt;";
        elLog.innerHTML += "msg content: name = " + msg.name + ", count = " + msg.count + "&lt;br/&gt;&lt;br/&gt;";
    }
}
myApp.init();</pre>
<p><a href="http://julianwong.net/samples/custom-event/dom.html">The sample in action</a></p>
<p>My sample code above works but with some draw backs:<br />
1. The custom event need to associated with a dom element which have nothing to do with it, here is the body element.<br />
2. When dealing with a large application / working with different plugin / mashup, we could end up with a name collision on the custom event.<br />
<a name="jQuery.mhub"></a></p>
<h3>Using the jQuery plugin jQuery.mhub</h3>
<p>To tackle the problems listed above, I created a <a href="http://plugins.jquery.com/project/mhub">message hub plugin, jQuery.mhub</a>. This jQuery.mhub employ a factory design. It only contains one single method:</p>
<pre>var demoHub = jQuery.mhub.create();</pre>
<p>Which return a message hub object with the following methods:</p>
<pre>demoHub.add(String NAME);
demoHub.remove(String NAME);
demoHub.send(String NAME, Object MESSAGE );
demoHub.listen(String NAME, Function HANDLE_FN, Object SCOPE );</pre>
<p>Here is a <a href="http://www.julianwong.net/samples/custom-event/mhub.html">sample</a> to see how the jQuery.mhub works. By using the jQuery.mhub, the above 2 problems state can be solved.</p>
<p>1. The custom event no longer tide up with a dom element.<br />
2. mhub object are created from jQuery.mhub.create();, you can get your own mhub without sharing it to other component and avoid name collision</p>
<p>A primitive <a href="http://www.julianwong.net/samples/custom-event/mhub.html">sample of jQuery.mhub</a>.</p>
<p>The source code:</p>
<pre>var count = 0;
var elLog = document.getElementById("log");
var demoHub = jQuery.mhub.create();

<strong>demoHub.add("ticker");</strong>

setInterval(function() {
    <strong>demoHub.send("ticker",{name:"my name is foo",count:count++});</strong>
},3000);

<strong>demoHub.listen("ticker", globalFunc);</strong>

function globalFunc(evt,msg) {
    elLog.innerHTML += "globalFunc triggered&lt;br/&gt;&lt;br/&gt;";
}

var myApp = {
    name:"app1",

    init:function() {
        <strong>demoHub.listen("ticker", this._handlerTicker, this);</strong>
    },

    _handlerTicker:function(name,msg) {
        elLog.innerHTML += this.name + "&lt;br/&gt;";
        elLog.innerHTML += "event name: " + name + "&lt;br/&gt;";
        elLog.innerHTML += "msg content: name = " + msg.name + ", count = " + msg.count + "&lt;br/&gt;";
    }
}

myApp.init();</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.julianwong.net/blog/2009/09/using-custom-event-on-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

