<?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>The Lucid &#187; jQuery</title>
	<atom:link href="http://thelucid.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com</link>
	<description>The Lightweight Ramblings of Jamie Hill</description>
	<lastBuildDate>Thu, 26 Jan 2012 13:52:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Super simple jQuery templating</title>
		<link>http://thelucid.com/2010/07/27/super-simple-jquery-templating/</link>
		<comments>http://thelucid.com/2010/07/27/super-simple-jquery-templating/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 17:41:46 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[templating]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=628</guid>
		<description><![CDATA[Working with jQuery, it is common to find yourself needing some kind of templating solution. There are plenty out there, however sometimes the use-case is simple enough that you don&#8217;t really need a fully blow templating plugin. We&#8217;ll take a task list as an example (just to be original). Lets say you have the following [...]]]></description>
			<content:encoded><![CDATA[<p>Working with <a href="http://jquery.com">jQuery</a>, it is common to find yourself needing some kind of templating solution. There are <a href="http://ejohn.org/blog/javascript-micro-templating/">plenty</a> out <a href="http://github.com/trix/nano">there</a>, however sometimes the use-case is simple enough that you don&#8217;t really need a fully blow templating plugin.</p>
<p>We&#8217;ll take a task list as an example (just to be original). Lets say you have the following task objects in an array:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> tasks <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
  <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'A todo item'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Another todo'</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you want to display them in an unordered list with checkboxes etc. What I like to do is create a &#8216;templates&#8217; object, which contains my basic templates:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> templates <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  taskList<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;ul class=&quot;task-list&quot;&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  taskItem<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li&gt;&lt;input type=&quot;checkbox&quot; /&gt; &lt;span /&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>With these templates in place, it&#8217;s a simple case of cloning them when required and inserting the relevant data. jQuery&#8217;s &#8216;<a href="http://api.jquery.com/text/">text</a>&#8216; and &#8216;<a href="http://api.jquery.com/attr/">attr</a>&#8216; methods are ideal for this as they handle the escaping of html entities. Based on this we can iterate over our tasks array and insert the relevant items in the dom:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> list <span style="color: #339933;">=</span> templates.<span style="color: #660066;">taskList</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>tasks<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>
  templates.<span style="color: #660066;">taskItem</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  list.<span style="color: #660066;">append</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;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And that&#8217;s it, by using jQuery&#8217;s &#8216;<a href="http://api.jquery.com/text/">text</a>&#8216; and &#8216;<a href="http://api.jquery.com/attr/">attr</a>&#8216; functions, you have complete control over your templates, without having to remember to escape html. Your templates can be stored away in an external javascript file and everyone&#8217;s happy.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2010/07/27/super-simple-jquery-templating/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;Ramble&#8221;, the Javascript Cucumber Port (work in progress)</title>
		<link>http://thelucid.com/2010/07/04/ramble-the-javascript-cucumber-port/</link>
		<comments>http://thelucid.com/2010/07/04/ramble-the-javascript-cucumber-port/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 02:27:12 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=608</guid>
		<description><![CDATA[I am a great fan of Cucumber when it comes to integration testing, however testing heavy use of Javascript can be a little tedious. I have looked into the different solutions out there such as Selenium but found them all to be fiddly to setup, however Capybara helps on this front. I was thinking, what [...]]]></description>
			<content:encoded><![CDATA[<p>I am a great fan of <a href="http://cukes.info/">Cucumber</a> when it comes to integration testing, however testing heavy use of Javascript can be a little tedious.</p>
<p>I have looked into the different solutions out there such as <a href="http://seleniumhq.org/">Selenium</a> but found them all to be fiddly to setup, however <a href="http://github.com/jnicklas/capybara">Capybara</a> helps on this front. I was thinking, what if Cucumber could run in the browser? No need for Javascript adapters or XML parsers, Safari/Chrome/Firefox already do a great job of this. Manipulating the page such as filling in forms, clicking links etc. could all be done with jQuery, in a very concise manor.</p>
<p>I decided to create a proof-of-concept while it was still fresh in my head. This is by no means a fully working release and the code leaves a lot to be desired in it&#8217;s current state, however it shows the benefits of a &#8220;Cucumber in the browser&#8221;. The main benefits I can see so far (for both javascript and non javascript apps) are:</p>
<ul>
<li><strong>Speed</strong> &#8211; browsers are getting extremely quick at this DOM stuff.</li>
<li><strong>Flexibility</strong> &#8211; everything happens client-side meaning you can easily test with any server technology.</li>
<li><strong>Simplicity</strong> &#8211; no need for complex javascript adapters, XML parsers etc.</li>
</ul>
<p>The basic file structure is very similar to Cucumber:</p>
<pre>
  - features
    index.html
    - js
        ramble.js
        jquery-1.4.2.js
      my.feature
    - steps
        web-steps.js
    - support
        paths.js
</pre>
<p>Step definitions can be defined in plain old javascript files with plain old jQuery, in this case web-steps.js. Currently the step definition are expected to throw an error if they cannot be fulfilled, this may change when a solid API is nailed down:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// The value of 'this' is the current document as a jQuery object.</span>
  ramble.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^I follow &quot;(.+)&quot;$/</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>link_text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</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> <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: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> link_text<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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">throw</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Can't find link: &quot;</span> <span style="color: #339933;">+</span> link_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    link.<span style="color: #660066;">click</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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Scenarios are exactly the same as in Cucumber, so you can do something like the following:</p>
<pre>
  Scenario: User fill out a form
    Given I am on the homepage
    And I follow "Tell us your name"
    And I fill in "First name" with "Jamie"
    And I fill in "Last name" with "Hill"
    And I press "Submit"
    Then I should see "Thank you for your details."
</pre>
<p>You can now simply drop the features folder into the public area of your app and visit the url in your browser. You should see the relevant steps go green or red as the app is navigated in an iFrame.</p>
<p>I plan to first tidy up the API (and unit test) and then aid the writing of scenarios by adding the ability to record them within the browser (Selenium style).</p>
<p>I&#8217;d like to hear peoples views on this&#8230; please don&#8217;t be too hard on the code, it&#8217;s more of a mind-dump than anything else at this stage (around 100 lines). There is an example of testing a static site included so just load the features/index.html file in your browser to see it run.</p>
<p>  <a href="http://github.com/soniciq/ramble">http://github.com/soniciq/ramble</a></p>
<p><strong>Update 04/07/10:</strong> As noted by Andrew in the comments, you will need a server running in order for browsers to get access to the pages for testing.</p>
<p>I have added a simple server script allowing the features to be run locally (requires Ruby). If you want to see it in action, just run:</p>
<pre>
cd /path/to/ramble/checkout
ruby server.rb
</pre>
<p>&#8230;and then visit <a href="http://localhost:1234/features">http://localhost:1234/features</a> in your browser (tested in Firefox, Chrome and Safari). Note that Ramble is not at all dependent on Ruby, it is just used for running a local test server.</p>
<h3>Screenshot</h3>
<p><a href="http://thelucid.com/files/ramble-in-action.png"><img src="http://thelucid.com/files/ramble-in-action-300x241.png" alt="Ramble in action" title="Ramble in action" width="300" height="241" class="aligncenter size-medium wp-image-620" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2010/07/04/ramble-the-javascript-cucumber-port/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>No &#8216;a:visited&#8217; in Safari 5&#8230; jQuery to the rescue?</title>
		<link>http://thelucid.com/2010/06/10/no-avisited-in-safari-5-jquery-to-the-rescue/</link>
		<comments>http://thelucid.com/2010/06/10/no-avisited-in-safari-5-jquery-to-the-rescue/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 15:36:12 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery safari javascript]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=597</guid>
		<description><![CDATA[With the release of Safari 5, it seems that the &#8216;:visited&#8217; CSS pseudo selector no longer takes affect. I completely understand it being removed due to breadcrumb sniffing but from a user&#8217;s perspective, differentiating between links that you have visited and those that you have not is very handy. jQuery to the rescue Using jQuery, [...]]]></description>
			<content:encoded><![CDATA[<p>With the release of <a href="http://www.apple.com/safari/">Safari 5</a>, it seems that the &#8216;:visited&#8217; CSS pseudo selector no longer takes affect. I completely understand it being removed due to <a href="http://ajaxian.com/archives/stop-sniffing-my-breadcrumbs">breadcrumb sniffing</a> but from a user&#8217;s perspective, differentiating between links that you have visited and those that you have not is very handy.</p>
<h3>jQuery to the rescue</h3>
<p>Using <a href="http://jquery.com/">jQuery</a>, we can easily add this functionality back in, without the XSS problems associated with the browser handling it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a:not(.visited)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visited'</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>You can then go ahead and set a different style for links that have the &#8216;visited&#8217; class.</p>
<p>The problem with this approach is that it will only work for links opened in a separate window or tab and not persist when you leave the current page, as all added classes will obviously be removed. There is probably a way to store a list of which links have been visited in a cookie, however each link would need a unique identifier&#8230; something for another time.</p>
<h3>Update</h3>
<p>It seems that the &#8216;:visited&#8217; selector does work in some cases in Safari 5 but not sure of the circumstances, can anyone shed any light on this?</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2010/06/10/no-avisited-in-safari-5-jquery-to-the-rescue/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery Colorbox within &#8220;overflow:hidden&#8221; container in Webkit</title>
		<link>http://thelucid.com/2009/12/29/jquery-colorbox-within-overflowhidden-container-in-webkit/</link>
		<comments>http://thelucid.com/2009/12/29/jquery-colorbox-within-overflowhidden-container-in-webkit/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 19:35:06 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[js jquery colorbox webkit fix bug]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=518</guid>
		<description><![CDATA[Wow, that&#8217;s a mouthful of a title. Just run into an interesting bug in Webkit I believe. When using Colorbox on a link contained inside a div who&#8217;s overflow attribute was set to hidden, the div was being scrolled to the bottom when hitting the &#8220;close&#8221; link of the colorbox. This only seemed to happen [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, that&#8217;s a mouthful of a title.</p>
<p>Just run into an interesting bug in Webkit I believe. When using <a href="http://colorpowered.com/colorbox/">Colorbox</a> on a link contained inside a div who&#8217;s overflow attribute was set to hidden, the div was being scrolled to the bottom when hitting the &#8220;close&#8221; link of the colorbox. This only seemed to happen in Webkit browsers.</p>
<p>The solution was to watch for a scroll event on the div in question and scroll back to 0:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
  <span style="color: #003366; font-weight: bold;">var</span> viewer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#viewer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  viewer.<span style="color: #000066;">scroll</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> viewer.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</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></pre></div></div>

<p>Obviously, this is only a fix when you require your content to always be scrolled to the top, however this could serve as a starting point for other scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2009/12/29/jquery-colorbox-within-overflowhidden-container-in-webkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

