<?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>SwiftLizard Interactive {Design, Development} &#187; jquery</title>
	<atom:link href="http://www.swift-lizard.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.swift-lizard.com</link>
	<description>all about a it - freelancers life</description>
	<lastBuildDate>Thu, 18 Mar 2010 09:10:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Development with jQuery &amp; Qunit</title>
		<link>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 01:13:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[agile development]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[qunit]]></category>

		<guid isPermaLink="false">http://www.swift-lizard.com/?p=172</guid>
		<description><![CDATA[Though a friend of mine and myself lately started again with continuous integration projects for the company&#8217;s we work for, and we both like test driven development, if it is no overkill, I came across Qunit which is a library provided by jQuery for Unittesting javascript. Until last week I did not even know something [...]]]></description>
			<content:encoded><![CDATA[<p>Though a friend of mine and myself lately started again with continuous integration projects for the company&#8217;s we work for, and we both like test driven development, if it is no overkill, I came across Qunit which is a library provided by jQuery for Unittesting javascript. Until last week I did not even know something like this existed for Javascript, but I am very happy that I came across this .  Because I think unittests are a good way to assure that your code works as expected, I´d like to show you how to setup and write jQuery unittests.<span id="more-172"></span></p>
<p>First of all you will need to download the following two files from the jQuery Api &#8211; Docs Website:</p>
<p><a href="http://github.com/jquery/qunit/raw/master/qunit/qunit.js" target="_blank">Qunit.js</a><br />
<a href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" target="_blank">Qunit.css</a></p>
<p>Afterwards you will need to create a new html file with the following code:</p>
<pre name="code" class="html">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;link rel="stylesheet" href="core/qunit.css" type="text/css"/&gt;
    &lt;script src="http://code.jquery.com/jquery-latest.js"&gt; &lt;/script&gt;
    &lt;script type="text/javascript" src="core/qunit.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
     // --- Unittest Code goes here ---
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1 id="qunit-header"&gt;QUnit example&lt;/h1&gt;
    &lt;h2 id="qunit-banner"&gt;&lt;/h2&gt;
    &lt;h2 id="qunit-userAgent"&gt;&lt;/h2&gt;
    &lt;ol id="qunit-tests"&gt;
    &lt;/ol&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>This Html file will be the page we can view the testresults on.</p>
<p>In this tutorial I only like to give you a brief overview  of what can be done how, because most of you will be familiar with writing unittests at other languages, and in the case of javascript and jQuery this will be almost the same.</p>
<p>So lets go write our first unittest:</p>
<pre name="code" class="javascript">  $(document).ready(function(){
    module("Basic Unit Test");
    test("Sample test", function()
    {
       expect(1);
       equals(divide(4,2),
        2,
        'Expected 2 as the result, result was: ' + divide(4,2));
    });
  });</pre>
<p>If you now open the the html page in your prefered browser you will see something like this:</p>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-00.40.58.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-193" title="Qunit first test" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-00.40.58-300x108.png" alt="Qunit first test" width="300" height="108" /></a></p>
<p>Your unittest failes, just like one might have expected because we never implemented the divide method into our code. But lets go step by step. You might have recognized several different parts of code in the js &#8211; snippet that need a little explanation.</p>
<p>First of all we have a line &#8220;module(&#8220;Basic Unit Test&#8221;);&#8221;. By adding this we are able to separate the modules we want to test visually on the test page. If you take a look on that page after running the tests you will read &#8220;<strong>1. Basic Unit Test module</strong>: Sample test (1, 0, 1)&#8221; at the moment. So the string we have set by adding &#8220;module(&#8220;Basic Unit Test&#8221;);&#8221; to our code is displayed there as well as some more information we have provided by the lines that followed this one.</p>
<p>Next line we have added so far was &#8220;test(&#8220;Sample test&#8221;, function(){});&#8221;. This is basically the declaration of a single test that you run. The first parameter is a String that simply gives the test a name(&#8220;1. Basic Unit Test module:<strong> Sample test</strong> (1, 0, 1)&#8221;), the second one is a callback function that is called the moment we run the test by loading the page.  Lets go on analyzing the code we just tested.</p>
<p>Next thing you might have recognized is &#8220;expect(1);&#8221;, this is a simple declaration how many assertions we expect at this single test. At the moment we run only one so this is set to one. If we had set this one to two for instance, the result of our test would be that not one assertion has failed but two, because in that case we would also have forgotten to implement the second assertion.</p>
<p>Lets move on to the last line we just implemented &#8220;equals(divide(4,2),2,&#8217;Expected 2 as the result, result was: &#8216; + divide(4,2));&#8221;.  This is the actual assertion we wanted to make, just to make sure that the method &#8220;divide&#8221; works properly and returns the mathematical correct result. The Qunit method equals takes three parameters. The first one and the second one are the ones compared, the third is a String, a message you might want to print out. This message is handled by the Qunit.log() method I will show you later.</p>
<p>Besides the method equals there are two more methods provided to you for testing. They are called &#8220;ok&#8221; and &#8220;same&#8221;. While &#8220;ok&#8221; is a simple Boolean check and for this only takes two parameters, the Boolean value that has to be true in order to not make the test fail, and a message, the &#8220;same&#8221; method is kind of like equal. The only difference between those two is that equal only returns true if both, the first and the second parameter, are 100% equal ( like a === b), and same only compares the variables values vs. each other.</p>
<p>But let me give you some example code:</p>
<pre name="code" class="javascript">  var arrayContainer = {a: 'firstElement'};
  equals(arrayContainer, {a: 'firstElement'}, 'Will fail');
  same(arrayContainer, {a: 'firstElement'}, 'Will pass');</pre>
<p>After describing the basics to you we can now start to make our test working. This is quite easy just add the following to your code:</p>
<pre name="code" class="javascript">  function divide(a,b)
  {
    return a / b;
  }</pre>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.24.46.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-202" title="Qunit test pass" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.24.46-300x115.png" alt="Qunit test pass" width="300" height="115" /></a></p>
<p>Your unit test passed, congratulations. But if you are a little bit like me you are missing the message we added to the equals assertion in our test, it is not displayed anywhere at our page, and even if you look at the console there will be nothing. This is because the public method Qunit.log() was indeed declared in the class file, but no code that would display the messages has been implemented. To my mind this was because the developers of Qunit wanted it to be up to your decision whether or not you like to display them.</p>
<p>It is done really easy and in this tutorial I will show you how to display them in the console of the browser (though it has one), just add the following on top of the ready callback :</p>
<pre name="code" class="javascript">  QUnit.log = function(result, message)
  {
    if (window.console &amp;&amp; window.console.log)
    {
      window.console.log(result +' :: '+ message);
    }
  }</pre>
<p>If you now reload the Unittest page,  open up Firebug for instance (in prior), and switch to the console view of Firebug you will see this:</p>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.38.33.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-205" title="Qunit Test with Firebug" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.38.33-300x150.png" alt="Qunit Test with Firebug" width="300" height="150" /></a></p>
<p>By now you should have the gear to develop some unittest for jQuery or any other javascript  you use might in your project. I hope this tutorial has given you a good overview and a nice point to start from. </p>
<p>The following link provides a zip file with the summary of this tutorial:<br />
<a href='http://www.swift-lizard.com/wp-content/uploads/2009/11/jQunit.zip'>jQunit-tutorial.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery sortable with Iframes in FireFox</title>
		<link>http://www.swift-lizard.com/2009/11/15/jquery-sortable-with-iframes-in-firefox/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://www.swift-lizard.com/2009/11/15/jquery-sortable-with-iframes-in-firefox/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 22:33:32 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://swift-lizard.com/?p=98</guid>
		<description><![CDATA[I came across this problem a week ago when I tried to develop an interface between an Iframe, or in Detail it&#8217;s content, and the iframes parent page. Turns out that the following code wouldn´t work in FireFox although all other browsers, yes even our friend the Internet Explorer, would behave as expected.
   [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem a week ago when I tried to develop an interface between an Iframe, or in Detail it&#8217;s content, and the iframes parent page. Turns out that the following code wouldn´t work in FireFox although all other browsers, yes even our friend the Internet Explorer, would behave as expected.<span id="more-98"></span></p>
<pre name="code" class="javascript">   function accessIframeJS(IframeName, methodName, methodParams)
   {
    var  IFrameObj  = window.frames[IframeName];
    var  methodCall = "IFrameObj."+methodName+"("+ methodParams +")";

    try{
       eval(methodCall);
    }catch(error){
       // some code here for error handling
    }
   }</pre>
<p>Traced on Firebug it turns out that FireFox loses the IFrameObj on Drag &amp; Drop while rewriting the DOM. Took me quite a while, and a long talk with uncle google until I took a look at bugzilla of mozzilla and the huge amount of iframe Bugs mentioned there. But I found the solution after reading alot of those posts,.. and yes as all ways it is an easy one, if you knew the solution.</p>
<pre name="code" class="javascript">function accessIframeJS(iframeId, methodName, methodParams)
 {
  var IFrameObj = document.getElementById(iframeId).contentWindow;
  var methodCall = "IFrameObj."+methodName+"("+ methodParams +")";

  try{
     eval(methodCall);
  }catch(error){
     // some code here for error handling
  }
 }</pre>
<p>Ta ta,&#8230; this works everywhere,&#8230; but it took me a long search, until I found the post in Bugzilla.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swift-lizard.com/2009/11/15/jquery-sortable-with-iframes-in-firefox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to develop a jQuery plugin</title>
		<link>http://www.swift-lizard.com/2009/11/15/how-to-develop-a-jquery-plugin/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://www.swift-lizard.com/2009/11/15/how-to-develop-a-jquery-plugin/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 14:15:47 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://swift-lizard.com/?p=51</guid>
		<description><![CDATA[Lately I started a new project at a German ISP where we (the team and me) have to develop several extensions to jQuery in order to build a framework to work with in this project. I am using jQuery for several years now but never had to develop my own extensions, because most of the [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I started a new project at a German ISP where we (the team and me) have to develop several extensions to jQuery in order to build a framework to work with in this project. I am using jQuery for several years now but never had to develop my own extensions, because most of the plugins available solved problems I came a cross. This time I have to do my own and started questioning uncle google about how to do it. No surprise there are several approaches to do a plugin for jQuery, but none of the tutorials I found answered all my Questions completely, so I decided to write this sum up of what I learned.</p>
<p><span id="more-51"></span></p>
<p>First of all you will need the basic wrapping:</p>
<pre name="code"  class="javascript">(function($){
   // your js code goes here
})(jQuery);</pre>
<p>This wrapping adds your code to the jQuery library if you add the js file to your page.<br />
Next thing you will have to do is add the basic functionality to your plugin.</p>
<pre name="code"  class="javascript">$.fn.yourPlugin = function(options){
}</pre>
<p>By adding those lines to your js file within the first code snippet you now can access your plugin by:</p>
<pre class="javascript">$('#yourDomElement').yourPlugin();</pre>
<p>Next you need to know the difference between private and public vars within the class you are developing. If you write something like:</p>
<pre name="code" class="javascript">   /**
     * default options for plugin
     * private
     * @var array defaultOptions
     */
    var defaultOptions = {
        action:       false,
        debug:        false
    }</pre>
<p>This would be a private var witch is only accessible within the class it self. If you need to have a var of your class to be public you have to write something like this:</p>
<pre name="code" class="javascript">   /**
     * default options for plugin
     * public
     * @var array defaultOptions
     */
    $.fn.yourPlugin.defaultOptions = {
        action:       false,
        debug:        false
    }</pre>
<p>One word has to be said by now, public variables can only be defined after the declaration of the constructor of your plugin, otherwise the compiler will throw a nice little javascript error because &#8220;yourPlugin&#8221; was not defined yet.<br />
Now that we have learned this we can go on with the possibility of configuration for the plugin. Like in every other language we will need to have some basic properties for the class that can be overwritten by your needs. To do something like that is quite simple:</p>
<pre name="code" class="javascript">   /**
     * yourPlugin base constructor
     *
     * @param [] options
     * @return void
     */
    $.fn.yourPlugin = function(options){
        // extend default options with overrides
        var opts = $.extend({}, defaultOptions, options);
    }</pre>
<p>So by now you can set the options you need by writing something like this:</p>
<pre name="code" class="javascript">   $('#yourDomElement').yourPlugin({
      action:       myAction
    });</pre>
<p>In this case you would have set opts.action from &#8220;false&#8221; to &#8220;myAction&#8221;.<br />
Next  we will go to private and public class methods. It is allmost the same as defining public and private member variables of your class.<br />
Something like this would be private:</p>
<pre name="code" class="javascript">   /**
     * private
     * method to do some vodoo
     */
    function yourPrivateClassMethod()
    {
        // the methods code
    }</pre>
<p>And something like this is a public method:</p>
<pre name="code" class="javascript">   /**
     * public
     * method to do some vodoo
     */
    $.fn.yourPlugin.yourPublicClassMethod = function()
    {
        // the methods code
    }</pre>
<p>Same rules like for the public member variables,&#8230; they have to be declared after your constructor, otherwise you will get an error. Another word of advise,.. as a developer I thought to access variables and methods within a class I can go by &#8220;this&#8221;,.. forget &#8220;this&#8221;,.. private methods and vars are accessed directly by their name, the public one you can access either by $(this).yourPlugin.yourPublicClassMethod() or $.fn.yourPlugin.yourPublicClassMethod().</p>
<p>So by now, if we put together the things we learned, we can do something like this:</p>
<pre name="code" class="javascript">(function($){
   /**
     * default options for plugin
     * private
     * @var array defaultOptions
     */
    var defaultOptions = {
        action:       false,
        debug:        false
    }

    /**
     * yourPlugin base constructor
     *
     * @param [] options
     * @return void
     */
    $.fn.yourPlugin = function(options){
        // extend default options with overrides
        var opts = $.extend({}, defaultOptions, options);

        switch(opts.action)
        {
        	case 'private':
        		yourPrivateClassMethod();
        		break;

        	case 'public':
        		$.fn.yourPlugin.yourPublicClassMethod();
        		break;
        }
    }

    /**
     * public
     * method to do some vodoo
     */
    $.fn.yourPlugin.yourPublicClassMethod = function()
    {
    	// should alert 'false', 'false',
    	//because the public vars where not set

        alert($.fn.yourPlugin.defaultOptions.action);
        alert($.fn.yourPlugin.defaultOptions.debug);
    }

    /**
     * default options for plugin
     * public
     * @var array defaultOptions
     */
    $.fn.yourPlugin.defaultOptions = {
        action:       false,
        debug:        false
    }

    /**
     * private
     * method to do some vodoo
     */
    function yourPrivateClassMethod()
    {
        alert('This method is private');
    }

})(jQuery);</pre>
<p>I hope this little summary helped a little in understanding how javascript classes and jQuery work.<br />
If you have any suggestion or did see some errors in this tutorial please let me know about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swift-lizard.com/2009/11/15/how-to-develop-a-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

