<?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>Tom&#039;s Blog &#187; Javascript</title>
	<atom:link href="http://www.encapsulated.org/blog/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.encapsulated.org/blog</link>
	<description>An analog guy in a digital world</description>
	<lastBuildDate>Tue, 15 Dec 2009 20:46:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unit testing your Javascript: Just Do It</title>
		<link>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 20:26:12 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=99</guid>
		<description><![CDATA[Nike&#8217;s slogan is the only one fit for the issue I&#8217;m about to raise in this article: Unit testing. Deep down, everyone knows the benefits from unit testing your code. Unit testing can give you that warm feeling when you go to bed, knowing that the changes you made, didn&#8217;t break previously working code. It [...]]]></description>
			<content:encoded><![CDATA[<p>Nike&#8217;s slogan is the only one fit for the issue I&#8217;m about to raise in this article: Unit testing. Deep down, everyone knows the benefits from unit testing your code. Unit testing can give you that warm feeling when you go to bed, knowing that the changes you made, didn&#8217;t break previously working code. It makes you happy and it gives you confidence.</p>
<p>Yet, a lot of us (including me) don&#8217;t actually start unit testing our code. There are a number of reasons for that, but I&#8217;ll have none of that now. Because now, I&#8217;m going to show you just how easy it is to unit test your Javascript. I&#8217;ll be using <a href="http://docs.jquery.com/QUnit">QUnit</a>, made by John Resig, creator of jQuery. Even though I&#8217;ve just begun using QUnit, the code I presented in my previous three blog posts, has really benefitted greatly from it.</p>
<p><span id="more-99"></span></p>
<h2>Setting up the environment</h2>
<p>To set up your basic unit testing environment, you won&#8217;t need to go through a lot of trouble. It&#8217;s as easy as creating an HTML page which loads the latest jQuery instance, QUnit instance and a stylesheet.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
                    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;nl&quot; lang=&quot;nl&quot;&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
        &lt;title&gt;Tag library Unit Tests&lt;/title&gt;
        &lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1 id=&quot;qunit-header&quot;&gt;Tag library Unit Tests&lt;/h1&gt;
        &lt;h2 id=&quot;qunit-banner&quot;&gt;&lt;/h2&gt;
        &lt;h2 id=&quot;qunit-userAgent&quot;&gt;&lt;/h2&gt;
        &lt;ol id=&quot;qunit-tests&quot;&gt;&lt;/ol&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>At this point, nothing much will be shown when you run the HTML file in your browser, as we still have to add our unit tests.</p>
<div class="wp-caption alignnone" style="width: 734px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-20.20.34.png" border="0" alt="Empty QUnit page" width="724" height="153" align="left" /><p class="wp-caption-text">Environment without tests</p></div>
<h2>Adding Unit Tests</h2>
<p>Now that you&#8217;ve seen how easy it is to set up your testing environment, it&#8217;s time to actually write a test. I will provide you with some samples from what I&#8217;ve used to test the Tag Javascript library. If you need an API, or you want to know all the possibilities, just head over to the QUnit website.</p>
<p>To keep my stuff organized, I try to reflect the regular structure as much as possible in my tests. This is what I mean:</p>
<div class="wp-caption alignnone" style="width: 231px"><img style="border: 0pt none; display: block;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-20.29.20.png" border="0" alt="Unit test file structure" width="221" height="251" align="left" /><p class="wp-caption-text">File structure</p></div>
<p>All tests are in separate files, reflecting the original structure. The tests for /tag/tag.pubsub.js are in the file /tests/tag/tag.pubsub.js. I think it&#8217;s a good idea to keep your testing in modules. One big file, or even in the environment HTML file would also work. It&#8217;s just not that easy to work with if you have a lot of tests.</p>
<p>Since I&#8217;m all for the modular way, first thing we add is an indication that a new module is tested</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</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;">/**
     * Define the module:
     */</span>
    module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag base class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This goes in the file /tests/tag.js (see image above), and will be referenced in the HTML environment we set up as our first step:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
                    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;nl&quot; lang=&quot;nl&quot;&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
        &lt;title&gt;Tag library Unit Tests&lt;/title&gt;
        &lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.js&quot;&gt;&lt;/script&gt;
&nbsp;
        &lt;!-- add links to unit tests below: --&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;../tag.js&quot;&gt;&lt;/script&gt;&lt;!-- library being tested --&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;tag.js&quot;&gt;&lt;/script&gt;&lt;!-- test code for library --&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1 id=&quot;qunit-header&quot;&gt;Tag library Unit Tests&lt;/h1&gt;
        &lt;h2 id=&quot;qunit-banner&quot;&gt;&lt;/h2&gt;
        &lt;h2 id=&quot;qunit-userAgent&quot;&gt;&lt;/h2&gt;
        &lt;ol id=&quot;qunit-tests&quot;&gt;&lt;/ol&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>In the environment, we first include the file containing the code to be tested. Next we include the file containing the tests for that code. You can add other files for each plugin you want to test.</p>
<p>Ok, our module is defined. The first test I&#8217;m adding is to verify if the file was loaded, and if the Tag namespace is defined in the window scope.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Define the module:
     */</span>
    module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag base class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Verify existence of the Tag object:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag&quot;</span><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>
        expect<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// optional</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of Tag object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">constructor</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying constructor type&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The tests to verify the existence of the Tag object are grouped inside a single test() case. Inside the test case, we can have as many assertions as we want. Here I have added 2 assertions: I verify if the type of window.Tag is an object, and if the constructor is a function. If both these assertions succeed, then that&#8217;s enough proof for me that the Tag base class is loaded and available on the page.</p>
<p>The basic premise of unit testing is that you test the smallest unit that makes up your code. In the case of the Tag library, the smallest unit will be a method from a plugin or the base class. So the next thing we&#8217;re obviously going to do, is test each of the methods inside the Tag object.</p>
<p>As an example, here are the assertions I&#8217;ve written to test the extend() functionality of Tag:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Verify working of extend method:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.extend()&quot;</span><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;">// an assertion using ok()</span>
        ok<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;Method exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// a dummy temporary plugin (also called a mock)</span>
        <span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            _initCheck<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            init<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; font-weight: bold;">this</span>._initCheck <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Verify reserved namespaces:</span>
        <span style="color: #003366; font-weight: bold;">var</span> reserved <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;extFN&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;extLocalFN&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>reserved<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>
            ok<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' cannot be overwritten&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// add test plugin</span>
        ok<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace 'test' taken&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ok<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace 'test' cannot be taken twice&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Tag.test plugin exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">test</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Test plugin added to FN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// verify if plugin is extended with extra functionality:</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>reserved<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">,</span> ns<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span><span style="color: #009900;">&#91;</span>ns<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>obj<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>
                same<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> obj<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verified existence of '&quot;</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// verify if init() was called:</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span>._initCheck<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Init was run&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The extend() method does a lot of things, naturally, all these things need to be tested. It&#8217;s the only way to be sure that the method does what it promises to do. For complex methods, with lots of execution paths, it will become difficult to test everything. That&#8217;s a reason to keep your methods small, and work with subroutines: they&#8217;re just easier to test (and to understand half a year after you last touched that part of the code).</p>
<h2>Unit testing AJAX calls</h2>
<p>The Tag.core plugin has functionality to asynchronously load javascript files. The problem with AJAX is that it is asynchronous, while unit testing is synchronous. The tests will all be executed in order, no test will run in parallel with another test. Yet we still need to be able to test asynchronous calls. QUnit provides us with the asyncTest() method for this purpose. I haven&#8217;t used that method though, it&#8217;s possible to do it &#8220;manually&#8221;.</p>
<p>We just need to pause the chain of tests before our asynchronous test. Once that test is completed, we can resume the rest of the tests. This is basically just waiting for an asynchronous test to complete, so all things considered, we test an asynchronous test synchronously. Nothing will happen in parallel.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Verify working of loadJS method:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.core.loadJS()&quot;</span><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;">// verify loading:</span>
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dummy.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// when testing asynchronous functionality, we must temporarily stop the</span>
        <span style="color: #006600; font-style: italic;">// running of the tests:</span>
        <span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</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>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// start the rest of the tests:</span>
            start<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><span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First, the chain of tests is paused via the stop() method. As soon as the test is done, the start() method will resume the normal chain of events. Simple but effective.</p>
<p>The asyncTest() method from QUnit does exactly the same as we have done here. It just executes the stop() method for you. You only need to indicate when to resume the tests.</p>
<p><strong>Beware</strong>, an asynchronous test may only contain 1 asynchronous call. E.g. not executing 2 AJAX calls in 1 test but rather just 1. The test itself can contain multiple assertions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.core.loadJS() multiple&quot;</span><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;">// verify loading multiple:</span>
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;dummy.js&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;dummy2.js&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</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>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded second dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// start the rest of the tests:</span>
            start<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><span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The test results</h2>
<p>While writing tests, you will want to see some results. Just save your files, and open the environment HTML file in your browser. The tests will be run, and you&#8217;ll be presented with the results:</p>
<div class="wp-caption alignnone" style="width: 722px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-21.09.17.png" border="0" alt="Unit test results" width="712" height="737" align="left" /><p class="wp-caption-text">Complete results, no errors</p></div>
<p>All failed assertions are marked in red. Any test can be clicked to see in a more detailed manner which assertions had what response:</p>
<div class="wp-caption alignnone" style="width: 723px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-21.12.39.png" border="0" alt="Failed unit test detail" width="713" height="583" align="left" /><p class="wp-caption-text">Suite with errors and detail</p></div>
<p>If you provided meaningful descriptions in your tests, it should be easy to see where an assertion failed.</p>
<h2>Conclusion</h2>
<p>Unit testing your Javascript functionality couldn&#8217;t be made easier. Getting started shouldn&#8217;t be the threshold, it&#8217;s as easy as creating one HTML file. Writing tests can be a bit of an adventure. At first you&#8217;ll probably make some mistakes, but that&#8217;s okay. The more you write tests, the more efficient you&#8217;ll make them. But you have to start somewhere. Just practice.</p>
<p>Remember, I can only show you the door. You have to walk through it. <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>All code from previous three blog posts now has a complete coverage from unit tests. You can still see the code at my <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">Codaset repository</a>. Please, feel free to download, fork, comment, &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 3)</title>
		<link>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 16:59:34 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=93</guid>
		<description><![CDATA[Goal
In this final part of the series, I&#8217;m going to create a Publish/Subscribe provider for my new framework. With this so-called pubsub provider, it will be possible for any function to create or fire an event. Any other function may then listen for that event, and will get triggered when the event occurs. My main [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>In this final part of the series, I&#8217;m going to create a Publish/Subscribe provider for my new framework. With this so-called pubsub provider, it will be possible for any function to create or fire an event. Any other function may then listen for that event, and will get triggered when the event occurs. My main goal is to make sure that this provider is easy to use, not only for plugins, but also for regular functionality. Besides that, I also want to have my code as loosely coupled as possible. For that, I&#8217;m going to use a little bit of Dependency Injection + lazy loading. All this to make sure that plugins are as agnostic as possible about the outside world. If you want to know more, please read on!</p>
<p><span id="more-93"></span><br />
<h2>Updates to the base library</h2>
<p>I&#8217;m going to start with some amends to the functionality I have already created in the <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">first part</a> of the series. Don&#8217;t know if you remember, but there I created functionality that is copied inside each plugin that registers itself into the base class. One of these methods is called &#8220;toString&#8221;. During my testing, I have discovered that using that name is not such a good idea. When using <a href="http://getfirebug.com/">Firebug</a>, the &#8220;toString&#8221; method is constantly called when logging a method. That&#8217;s why I decided to rename the method to &#8220;_toString&#8221;.</p>
<p>Besides the name change, I have also added a local cache in the method. This is to make sure that the expensive for loop is executed only once. The result is then put in a local cache. On each subsequent call of &#8220;_toString&#8221;, the local cache is used.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Contains all plugins
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</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><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><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>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Cache object
             */</span>
            _cache<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<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>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            _toString<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;">// Because a loop is costly, we'll only do it once, and cache</span>
                <span style="color: #006600; font-style: italic;">// the result of the loop</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// the name is not cached, so we need to cache it:</span>
                    <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</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>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// notify the user that this is an unknown plugin</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</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;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// return the cached name:</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</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: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While I was busy refactoring the &#8220;toString&#8221; method, I added a &#8220;getParent&#8221; method too. This method should be used when you want to refer to the Tag base class from within a plugin, without having to know the name. The advantage is that when Tag gets renamed, the plugins still need very little change in their code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">            <span style="color: #006600; font-style: italic;">//... inside the extFN part of the base object:</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Returns the base object
             *
             * @return Object
             */</span>
            getParent<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; font-weight: bold;">return</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>For such a small method, I&#8217;m not going to repeat the entire base class again. Saves me some space, because this will be a long article <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p></p>
<h2>Creating the Pubsub plugin</h2>
<p>Now for the new code. We start of with our basic template again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here I do need the settings. It will be filled up a bit later. A Pubsub provider will have 3 basic methods. In other implementations, they may have different names than what I&#8217;ll use, but that&#8217;s not very important. The methods I need are these:</p>
<ul>
<li>A method to listen for an event: &#8220;subscribe&#8221;</li>
<li>A method to stop listening for an event: &#8220;unsubscribe&#8221;</li>
<li>A method to fire off an event: &#8220;notify&#8221;</li>
</ul>
<p>I&#8217;ll start with subscribing to an event. The premise here is that a method registers itself as a listener for a particular event. So I need to know: what event? what should happen when that event occurs?</p>
<p>I have to keep track of that information. For this purpose, I&#8217;m going to create 2 properties in the plugin: One for keeping track of what method should be called on what event. And another one to make sure a subscription can easily be undone.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<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: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<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: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</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: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let me walk you through the code. The properties for keeping track are added and I have introduced a setting. This setting will be used in the method &#8220;_constructPath&#8221;. The &#8220;subscribe&#8221; method is added, and takes 3 parameters: the plugin that will fire the event, the event name, and the function that should be executed when that event takes place.</p>
<p>First, I create a &#8216;path&#8217; from the plugin name and the event name. If the plugin is &#8216;Tag.autosuggest&#8217;, and the event is &#8216;gotResponse&#8217;, then the path will be &#8216;Tag.autosuggest.gotResponse&#8217;. I have created a method just to create the path. If I ever find out that the path should change, then I only need to change it in that one method. That&#8217;s called keeping your code <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.</p>
<p>Second, the properties are filled up with the necessary data. Each path will be stored in the &#8220;subscriptions&#8221; property, along with a list of functions that will be executed when that event occurs. If two listeners subscribe to an event, then the index of that event will have a list of 2 functions.</p>
<p>Third, to make it&#8217;s easy to unsubscribe from an event, a second list is kept. Each subscription gets an entry in that list. The index of that subscription is then returned at the end of the &#8220;subscribe&#8221; method. With this ID, it&#8217;s possible to unsubscribe without having to specify the plugin and event.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<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: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<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: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Cancels a subscription to a given notification
         *
         * @param id Integer - ID of the subscription
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        unsubscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// don't remove it with splice, or other id's will be FUBAR</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">path</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><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>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Unable to remove this subscription'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</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>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</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: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The &#8220;unsubscribe&#8221; method removes the entries from our lists. Via the given ID, it looks up where the callback function is listed in the &#8220;subscriptions&#8221; property. It is <strong>very</strong> important that we don&#8217;t physically remove the entry from the array. Instead, I just overwrite what was there with &#8220;null&#8221; values.</p>
<p>If we remove the entries via &#8220;Array.splice&#8221;, then the array will be re-indexed. This will invalidate all other ID&#8217;s, so we can&#8217;t use &#8220;Array.splice&#8221;. Using the regular &#8220;delete&#8221; method&#8230;. well, let&#8217;s just say that in this case it would work, but I&#8217;m not too fond of using &#8220;delete&#8221; with arrays. It doesn&#8217;t actually delete the entry, it just writes null values to that index. Yeah, that&#8217;s exactly what I&#8217;m doing manually. But that&#8217;s not called &#8220;deleting an entry&#8221;, so let&#8217;s make sure we don&#8217;t get confused. We don&#8217;t want to end up refactoring &#8220;delete&#8221; with &#8220;Array.splice&#8221;, because we forgot that we don&#8217;t want an actual delete.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<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: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<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: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Cancels a subscription to a given notification
         *
         * @param id Integer - ID of the subscription
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        unsubscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// don't remove it with splice, or other id's will be FUBAR</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">path</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><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>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Unable to remove this subscription'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</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>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Send out a notification to all subscribers
         *
         * @param plugin String - Name fo the plugin
         * @param evt String - Name of the event
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        notify<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// make sure we only execute functions, not strings, integers, null's, ...</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</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: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With the &#8220;notify&#8221; method, you fire off an event. There are two arguments: name of the plugin and name of the event. From these two arguments, the path is constructed again. Then I look up in the &#8220;subscriptions&#8221; list if that entry exists. If it does, then all functions listed there will be executed.</p>
<p>The &#8220;notify&#8221; method works as a fire-and-forget system. If there are no listeners for an event, then nothing will happen. There is no communication back to the method that fired the event. That&#8217;s not necessary. It&#8217;s not its task to know if the event will be correctly dispatched.</p>
<p>At the moment, &#8220;notify&#8221; doesn&#8217;t transmit a payload to the subscribers. I&#8217;m sure I will need that functionality eventually. I&#8217;ll add it later, when the need is actually there.</p>
<p></p>
<h2>The problem of scope</h2>
<p>When a method subscribes to an event, and then gets executed, there will be a problem with the scope of the method. The Pubsub plugin doesn&#8217;t know anything about the callback function, other than it is a function. It can either be an anonymous function, or it can be part of an object. Pubsub doesn&#8217;t know. Hence, when the function is called, there won&#8217;t be a scope. The scope is the function itself. For an anonymous function, that&#8217;s good. For a method that&#8217;s part of an object, that&#8217;s not good</p>
<p><strong>Possible solution 1</strong>: Just execute the callback in the scope of the Pubsub provider, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ...</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// ...</span></pre></div></div>

<p>This is not a good solution, because anonymous functions suddenly have a scope and methods from an object suddenly have a scope that isn&#8217;t theirs. If the method is &#8216;Tag.autosuggest.updateSuggestions&#8217;, then the scope should be &#8216;Tag.autosuggest&#8217;, and not &#8216;Tag.pubsub&#8217;.</p>
<p><strong>Possible solution 2:</strong> Execute the callback in the scope of the notifier. Via some code, it should be easy to determine the scope of the notifier. But again, my same objections apply as with the first solution: Anonymous functions shouldn&#8217;t have a scope, and I don&#8217;t want to change the scope of methods from an existing object</p>
<p><strong>Possible (and accepted) solution 3:</strong> Make sure the callback already has the correct scope before executing it. (see below)</p>
<p></p>
<h2>Hitching a ride</h2>
<p>(I told you this was going to be a long article) The idea comes from &#8220;dojo.hitch&#8221;: Via another method making sure that a given function is executed in a given scope. So I created &#8220;Tag.core.hitch&#8221; for this purpose. The name comes from <a href="http://www.dojotoolkit.org/">Dojo</a>, and the implementation is derived from dojo&#8217;s implementation.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #006600; font-style: italic;">// inside the Tag.core plugin:</span>
        <span style="color: #006600; font-style: italic;">/**
         * Runs fn in the given scope
         *
         * @param scope Object - The scope fn should run in
         * @param fn Function|String - The function to run
         *
         * Extra parameters are optional, and will be passed on to fn
         *
         * @return Function
         */</span>
        hitch<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// arguments looks like an Array, but is an Object</span>
            <span style="color: #006600; font-style: italic;">// here we convert it to an array</span>
            <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> scope <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span> fn <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// both scope &amp; fn should be defined</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Tag.hitch: both scope and function should be defined'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> fn <span style="color: #339933;">===</span> <span style="color: #3366CC;">'string'</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><span style="color: #000066; font-weight: bold;">typeof</span> scope<span style="color: #009900;">&#91;</span>fn<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</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;">'The function is not defined inside the given scope'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">return</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; font-weight: bold;">return</span> scope<span style="color: #009900;">&#91;</span>fn<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">return</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; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method returns an anonymous function. Inside that anonymous function, the scope of the given function is set to the scope that&#8217;s defined in the arguments. Quite simple actually. But very effective.</p>
<p>With the hitch method, a subscriber is now in charge of making sure that the callback function is executed in the correct scope.</p>
<p></p>
<h2>Dependency Injection</h2>
<p>Before I come to the usage examples, there is one more thing I&#8217;d like to implement. You know how I like keeping my plugins agnostic. Well, instead of letting a plugin use the pubsub provider like this: &#8220;Tag.pubsub.notify(&#8230;)&#8221;, I want to make sure that it&#8217;s not important that the pubsub provider is called &#8220;Tag.pubsub&#8221;. In fact, if someone comes along and writes a better provider, it should be possible to use that provider without having to change the code of the plugins.</p>
<p>This can be accomplished with a little Dependency Injection. (DI via getters and setters to be precise). I&#8217;ll extend core plugin with 2 methods: &#8220;getPubsubProvider&#8221; and &#8220;setPubsubProvider&#8221;. With the getter, the Pubsub provider is returned. Via he setter it&#8217;s possible to specify another Pubsub provider. As the icing on the cake, I&#8217;ll add some lazy loading: if no pubsub provider is specified, then &#8220;getPubsubProvider&#8221; defaults to &#8220;Tag.pubsub&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Contains all plugins
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</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><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><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>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Cache object
             */</span>
            _cache<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Pubsub provider
             */</span>
            pubsubProvider<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<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>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Inject another Pubsub Provider
             *
             * @param obj Object - The new Pubsub Provider
             *
             * @return Object - Current plugin for chainability
             */</span>
            setPubsubProvider<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// verify if necessary functionality is present:</span>
                <span style="color: #003366; font-weight: bold;">var</span> fns <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;subscribe&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;unsubscribe&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;notify&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> validObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>fns<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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> obj<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        validObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>validObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <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>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Get the Pubsub Provider
             *
             * @return Object - Pubsub Provider
             */</span>
            getPubsubProvider<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;">// lazy loading:</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">pubsub</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Returns the base object
             *
             * @return Object
             */</span>
            getParent<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; font-weight: bold;">return</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            _toString<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;">// Because a loop is costly, we'll only do it once, and cache</span>
                <span style="color: #006600; font-style: italic;">// the result of the loop</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// the name is not cached, so we need to cache it:</span>
                    <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</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>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// notify the user that this is an unknown plugin</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</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;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// return the cached name:</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</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: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s the entire base class for now. As you can see, &#8220;setPubsubProvider&#8221; verifies if the new provider has all three required methods. I added this so that the plugins don&#8217;t have to be rewritten. If a new Pubsub Provider is introduced, with different names for the methods, then all you have to do is write an adapter for it, and set it via &#8220;setPubsubProvider&#8221;.</p>
<p></p>
<h2>Usage</h2>
<p>A lot of code has been written and added. Here are some examples how all this can be used. For most examples, I&#8217;ll use a fictive &#8220;Tag.autosuggest&#8221; plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// inside the Tag.autosuggest plugin, we throw an event:</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(you see, no &#8216;Tag&#8217; mentioned here. I like!)</p>
<p>Inside another plugin, we listen for the above event:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><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;">'Treshold has been reached'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Still no mention of &#8216;Tag&#8217; here. True, we have to write a lot of code, but it&#8217;s future proof)</p>
<p>Instead of an anonymous function, I want to execute a method of an object. We have to keep scope here:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbackFunction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(&#8220;callbackFunction&#8221; will be executed in the correct scope)</p>
<p>An alternative to the above is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;callbackFunction&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Here, &#8220;callbackFunction&#8221; is specified as a string. Other than that, the exact same will happen as the previous example)</p>
<p>Unsubscribe from an event:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbackFunction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All above examples implied another plugin. But the code can also be used in regular Javascript, provided that Tag and Tag.pubsub are available on the page.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> doCalculation <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Tag.<span style="color: #660066;">pubspub</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'beforeCalculation'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
    Tag.<span style="color: #660066;">pubspub</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'afterCalculation'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> Tag.<span style="color: #660066;">pubsub</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'afterCalculation'</span><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;">'Calculation complete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Tag.<span style="color: #660066;">pubsub</span>.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Instead of the plugin name, I provided the function name to the &#8220;notify&#8221; and &#8220;subscribe&#8221; methods.</p>
<p></p>
<h2>Conclusion</h2>
<p>Finally, the end of a lengthy article. With the pubsub plugin, it should be easy to make your plugins event-driven, and loosely coupled. Already, a shortcoming is visible: It should be possible to transmit data from the notifier to the subscriber. In the coming days or maybe next week, I&#8217;ll refactor the code to allow that. Once that feature is added, it will be possible to write your application as a <a href="http://en.wikipedia.org/wiki/Blackboard_system">blackboard system</a>, with all functionality independent from each other.</p>
<p>In these articles, I have introduced some principles from the OO world, like Dependency Injection, modular design, lazy loading, DRY, &#8230; If you want to read them again, the <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">first article is available here</a>, and the <a href="http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/">second article is available here</a>. The code of this library is under constant development. I&#8217;ve made it available under the MIT license on my Codaset account on <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a>. Feel free to fork it, add change requests or bug reports. If you want to use the code, and you create plugins for it, please let me know. I&#8217;m curious if and how this will be used by other people.</p>
<p>Thank you for reading my article. Hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 2)</title>
		<link>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 16:15:53 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=91</guid>
		<description><![CDATA[Goal
In this second installment of my 3-part library-building journey, I&#8217;m going to make the core plugin for the library. This plugin will contain utility methods. Stuff that can be used, either by other plugins, or just by regular Javascript code. So far, I haven&#8217;t needed many utility methods, so this chapter will be rather short. [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>In this second installment of my 3-part library-building journey, I&#8217;m going to make the core plugin for the library. This plugin will contain utility methods. Stuff that can be used, either by other plugins, or just by regular Javascript code. So far, I haven&#8217;t needed many utility methods, so this chapter will be rather short. If you&#8217;re ready, read on!</p>
<p><span id="more-91"></span><br />
<h2>Creating the Core plugin</h2>
<p>So far, the only functionality I have needed, was dynamically loading other Javascript files on a page. jQuery is perfectly capable of doing just that. I&#8217;m just going to wrap it up in a method or two.</p>
<p>First, let&#8217;s start off with the base of my plugin. A self-contained, anonymous function that will be executed immediately.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">my previous blog post</a>, there were some extra&#8217;s there: settings and an init method. I won&#8217;t be needing those at this moment, so I left them out.</p>
<p>There are two important things I need when dynamically loading other Javascript files. I want to be able to do something when the file is loaded. And in some cases I want to make sure that a file is loaded only once. The reason for that last feature is that when I want to create self-contained <a href="http://framework.zend.com/manual/en/zend.view.helpers.html">partials</a>, I don&#8217;t want to load the same file over and over again when that partial is put on my page more than once.</p>
<p>That being said, the first method will accept a file URL and optionally a callback. The callback allows me to execute code after the file was loaded.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * Loads a JS library via Ajax and execute an optional callback function
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadJS<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<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: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                callback <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: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// load it via Ajax</span>
            $.<span style="color: #660066;">getScript</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<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: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I just used jQuery&#8217;s <a href="http://docs.jquery.com/Ajax/jQuery.getScript#urlcallback">getScript()</a> method, so all restrictions apply. The return value of the method is the current object or scope. This allows you to chain multiple methods, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/first.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/second.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">someOtherFunctionFromCore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This keeps it all nice and tidy. You can read up on chainability in Javascript here on <a href="http://javascriptant.com/articles/32/chaining-your-javascript-methods">Javascript Ant</a>. Chaining methods is commonly used in PHP as well. I like that principle, because it makes your code shorter and more readable.</p>
<p>Now that I can load 1 file, I want to be able to make sure it&#8217;s loaded only once. For that the plugin needs an extra method, and a list to put the loaded libraries in.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * @var array - List of loaded JS scripts
         */</span>
        libraries<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: #006600; font-style: italic;">/**
         * Loads a JS library via Ajax and execute an optional callback function
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadJS<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<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: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                callback <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: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// load it via Ajax</span>
            $.<span style="color: #660066;">getScript</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<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>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Loads a JS library once via Ajax, with optional callback
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadOnce<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<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: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                callback <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: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">libraries</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</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: #006600; font-style: italic;">// load JS:</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// append to list:</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">libraries</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><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>
                <span style="color: #006600; font-style: italic;">// just execute the callback when the library is already loaded</span>
                callback<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: #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: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The &#8220;loadOnce&#8221; method takes the same input as the &#8220;loadJS&#8221; method. But as a bonus, it keeps track of the files it has loaded. When some code is trying to load a file for the second time, then there is no penalty. The callback will just be executed, just as if it were the first time the file was loaded.</p>
<p></p>
<h2>Usage</h2>
<p>I&#8217;ll just give you some examples. Simplest case, you just want to load a file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a couple of files:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// OR</span>
	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a file, and then execute some code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><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;">'File is loaded'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a file after another file was loaded:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><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>
		Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a plugin, and then set the correct settings for it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/myPlugin.js'</span><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>
		Tag.<span style="color: #660066;">myPlugin</span>.<span style="color: #660066;">setConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			url<span style="color: #339933;">:</span><span style="color: #3366CC;">'http://www.example.com/'</span><span style="color: #339933;">,</span>
			selectors<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				exampleLink<span style="color: #339933;">:</span><span style="color: #3366CC;">'.example'</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The list could go on forever. All these examples are also valid for the &#8220;loadOnce&#8221; method.</p>
<p></p>
<h2>Next</h2>
<p>In the last part of this series, I&#8217;ll create my Pubsub plugin: an event dispatcher. For this, I will have to modify the base class a bit, as I want to make use of <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a>. But that&#8217;s for tomorrow.</p>
<p>All code is still available on my Codaset account here: <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 1)</title>
		<link>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/</link>
		<comments>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 18:39:56 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=89</guid>
		<description><![CDATA[Goal
There are two programming languages I like: PHP and Javascript. In this mini series, I will explore Javascript and jQuery, and build my own library/framework. I won&#8217;t re-invent the wheel here. I&#8217;ll just make a collection of methods and functionality I need, making use of some functionality in jQuery.
The goal of the first part of [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>There are two programming languages I like: PHP and Javascript. In this mini series, I will explore Javascript and <a href="http://jquery.com/">jQuery</a>, and build my own library/framework. I won&#8217;t re-invent the wheel here. I&#8217;ll just make a collection of methods and functionality I need, making use of some functionality in jQuery.</p>
<p>The goal of the first part of my mission is to create a small, lightweight, self-contained, easily extensible base for my framework. Recently I created my own little company, called &#8220;The Analog Guy&#8221;. I&#8217;ll just call my library &#8220;TAG&#8221;.</p>
<p><span id="more-89"></span><br />
<h2>Creating the base</h2>
<p>One thing I have learned from using other libraries, is that I don&#8217;t like initializing an object in Javascript. I just want to include the script on my page, and it has to work out of the box. I&#8217;ll use a <a href="http://javascriptant.com/articles/30/javascript-library-techniques-tips-and-tricks">self-executing anonymous function</a> to encapsulate my class, initialize it, and add some functionality. We start with the blueprint of the self-executing anonymous function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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;">// rest goes here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>This is an anonymous function that will be executed immediately. As a parameter to this anonymous function, I give the jQuery object. This makes sure that inside my anonymous function, $ refers to the jQuery object. Even if on the rest of the page, another library is used that defines $. That&#8217;s called the <a href="http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3">scope</a> of the object.</p>
<p>So well, next thing I want to do, is reserve my own TAG namespace. So I&#8217;ll add a little code for that:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// rest goes here</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>If another script uses the &#8220;Tag&#8221; namespace in the global scope, then that might indicate that the user has accidentally included the tag.js file twice on the same page. I&#8217;ve made sure that it the namespace only gets defined just once.</p>
<p>In my goal, I said I was going to build this library on top of jQuery. I want to make sure that $ is defined, and that it is jQuery. I don&#8217;t want to support other libraries at this moment.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</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;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now that I know that jQuery is available, the next thing I want to add, is extensibility. I want to keep the base lightweight: only add the functionality that is needed. All the other functionality, that is non-essential, should be added via plugins or extensions. This makes it possible:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</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><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</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;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The extend method has two arguments: the name of the namespace the plugin wants to use, and then the plugin itself. First there is a check if that namespace is still available. If it is, the first thing I do, is add some basic functionality to the plugin, that will make life easier for the developer of that plugin. Don&#8217;t worry, we&#8217;ll see immediately where I add that core functionality. A next step is to inject the plugin into the base class. For this, I use the &#8220;this.fn&#8221; object, because that makes it easier to detect which plugins are loaded. As a &#8220;bonus&#8221;, I also inject that same plugin in the base namespace. That makes it more convenient to use the plugin. &#8220;Tag.fn.myPlugin&#8221; can be referred to as &#8220;Tag.myPlugin&#8221;. Lastly, if that plugin needs some automagical setup, it can define the &#8220;init&#8221; method, which will get triggered when loading the plugin.</p>
<p>This provides basic extensibility for my framework. The next method I need, is a utility method. I cannot place it on a plugin, because it is something basic: a method that recursively sets a value inside an object</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</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><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><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>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>I know jQuery.extend() exists. I just don&#8217;t think it can do what I want: set some properties of an object, without touching other existing properties. The method &#8220;setObjectData&#8221; I just created will do just that. It recursively loops through a set of values, and sets these values on the given object. This method will be used later, for setting values in a plugin configuration.</p>
<p>For now, I&#8217;m happy with that functionality. I don&#8217;t have an immediate use for other functionality. So we can initialize our class now. Since this takes only 1 line of code, I&#8217;ll immediately add some more functionality.</p>
<p>There are certain methods, I want to be available in every plugin, without the plugin having to know the name of my base class. I think it&#8217;s a basic need of the plugins, to be as agnostic as possible about the rest of the world. The fewer the dependencies, the better. If I ever decide to rename my company, and I rename my base library. Then the plugin should still work with little or no effort. These methods are &#8220;setConfig&#8221;, to set values in a plugin config, and &#8220;toString&#8221;, to automagically determine the name of the plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</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; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <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;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</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;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<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: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</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><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><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>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<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>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            toString<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; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</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>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</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: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it for the moment. I&#8217;ll certainly need more functionality inside the base class, but I will build it was I need it.</p>
<p></p>
<h2>Usage</h2>
<p>Well, it&#8217;s a basic class really, there is no real usage for now. The strength will come from the plugins. To include the library in your files, simply include script tags which point to the correct JS file.</p>
<p></p>
<h2>Writing your own plugins</h2>
<p>Since I&#8217;m emphasizing plugins, I&#8217;ll give you the basic structure of how a plugin should be built. In future parts, I&#8217;ll build at least two plugins that will further extend the functionality: a &#8220;core&#8221; plugin, and a &#8220;pubsub&#8221; plugin. Here&#8217;s the template:</p>
<pre language="javascript">
(function(){
    var myPlugin = {
        /**
         * @var object settings - The configuration of this library
         */
        settings:{},

        /**
         * Will be called as soon as this library is loaded
         * into the main Tag library
         *
         * @return void
         */
        init:function(){},

        // other methods go below
    };
    Tag.extend("myPlugin",myPlugin);
})();
</pre>
<p>That&#8217;s all there is to it. Just an <a href="http://www.wait-till-i.com/2006/02/16/show-love-to-the-object-literal/">Object Literal notation</a> of an object, again inside a self-executing anonymous function. Include your JS file in your HTML, after you included the Tag library, and your plugin will be available as &#8220;Tag.myPlugin&#8221;.</p>
<p></p>
<h2>Next</h2>
<p>In the next part of this mini series, I&#8217;ll create the &#8220;core&#8221; plugin. This plugin will create functionality that is quite basic, but not essential to the library. Usually the methods will be utility methods which can be used by other plugins or regular Javascript code.</p>
<p>In the last part of the series, I&#8217;ll introduce my &#8220;pubsub&#8221; plugin. It&#8217;s a plugin that serves as a dispatcher for events. With it, it is possible to send out event notifications. Other parts of your code will be able to (un-)subscribe to events, and will get triggered when the event is fired.</p>
<p>If you have any remarks, please let me know. I&#8217;m open to suggestions and improvements. My code is available at <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
