<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1.1" -->
<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/"
	>

<channel>
	<title>Spadgos</title>
	<link>http://spadgos.com</link>
	<description>It's "Spad-joss"</description>
	<pubDate>Fri, 25 Nov 2011 08:55:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.1</generator>
	<language>en</language>
			<item>
		<title>Time to upgrade</title>
		<link>http://spadgos.com/?p=70</link>
		<comments>http://spadgos.com/?p=70#comments</comments>
		<pubDate>Fri, 25 Nov 2011 08:55:12 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=70</guid>
		<description><![CDATA[I&#8217;ve finally gotten tired of this Wordpress Blog and moved to a new one based on Octopress. The new site is temporarily hosted at http://spadgos.github.com. Eventually, it&#8217;ll also get moved over to spadgos.com and we can kiss this horrid red-and-black theme goodbye.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finally gotten tired of this Wordpress Blog and moved to a new one based on Octopress. The new site is temporarily hosted at <a href="http://spadgos.github.com/">http://spadgos.github.com</a>. Eventually, it&#8217;ll also get moved over to spadgos.com and we can kiss this horrid red-and-black theme goodbye.</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=70</wfw:commentRss>
		</item>
		<item>
		<title>Sublime Text &#038; JSDocs</title>
		<link>http://spadgos.com/?p=69</link>
		<comments>http://spadgos.com/?p=69#comments</comments>
		<pubDate>Mon, 31 Oct 2011 23:12:20 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[ramblings]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=69</guid>
		<description><![CDATA[Recently, I&#8217;ve had one of those events in my professional life. They happen (for me) even less frequently than changing jobs. Yes, I have changed my preferred text editor. gasp!
I&#8217;ve moved from Komodo, which is a very competent and extensible (with Javascript!) editor, to the new kid on the block, Sublime Text 2. Sublime is [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve had one of those events in my professional life. They happen (for me) even less frequently than changing jobs. Yes, I have changed my preferred text editor. <em>gasp!</em></p>
<p>I&#8217;ve moved from <a href="http://www.activestate.com/komodo-edit">Komodo</a>, which is a very competent and extensible <i>(with Javascript!)</i> editor, to the new kid on the block, <a href="www.sublimetext.com/">Sublime Text 2</a>. Sublime is a product still in beta, but I can recommend it as being definitely stable enough for every day use.</p>
<p><i>I&#8217;ll apologise up front if the following sounds too gushing or like an advertising pitch. I have no commercial interest in the product whatsoever, I actually just really like it and want others to try it out.</i></p>
<p>When starting with Sublime, the first thing you&#8217;ll notice is how amazingly fast it is. I am still amazed at how quickly it starts and continues to be, even on my little old netbook. The second thing is that you&#8217;ll see that Sublime is a text editor made by programmers for programmers. The default colour scheme is a very pleasing and eye-friendly pastels on dark grey, the interface is absolutely no-nonsense: no toolbars; no buttons for new file, save, cut, copy or paste; just one menu bar, an (albeit slightly gimmicky IMO) &#8220;minimap&#8221; of the file, and your code. All commands are available from a &#8220;command palette&#8221; which is opened via a key binding. Similarly, opening project files or searching for functions is done with another palette. These palettes, apart from being super-quick, also provide fuzzy text matching. For example, to make the selected text upper case, I just select the text, then press Ctrl+Shift+P, and type &#8220;chup&#8221;, which will find &#8220;CHange text to UPper case&#8221;, or if I want to open &#8220;lib/test/PolygonTest.js&#8221;, Ctrl+Shift+O and &#8220;l/t/p&#8221; or &#8220;PolTe&#8221; or whatever narrows it down enough with the minimum amount of typing. Everything is possible without even touching your mouse.</p>
<p>Probably the most surprising thing is the configuration settings. Every programmer worth his salt will fiddle and adjust settings to get the work environment Just Right, and usually this means poring through dialog after dialog of checkboxes and pull-downs. In Sublime, all the configuration (yes, ALL) is done via JSON text files. This is shocking at first, but amazingly liberating once you start to use it. As a programmer, every day you build complex data-structures capable of modelling anything by combining numbers and strings into nested arrays and maps &#8212; why couldn&#8217;t you configure a text editor in the same way? Settings are applied instantly upon save, meaning you can test out each of the settings very easily and get it just how you like it. Even key bindings are handled this way. Every interaction you have, from moving the cursor, to basic copy/paste/save actions, even to what happens when you click a mouse button is configurable. Even better though is that you can bind the same key combo to several actions and then affix conditions (referred to as &#8220;context&#8221; in the config) to that setting. For example, the &#8220;enter&#8221; key can add a new line in regular text, but you can also add another binding which makes it prepend commenting characters (//) if the current context is a comment. Triple-click might normally select a line, but if it&#8217;s on a function definition it could select the entire function body for you. After all this tweaking, you can commit your plain-text settings to version control for portability between your workstations, which is very handy!</p>
<p>One other feature I&#8217;ve not seen in any other editor is multiple selections. Komodo and other editors (VI, Emacs&#8230;) have &#8220;block editing&#8221; where you can make rectangular selections and edits across rows and columns. This is particularly useful for modifying blocks of repetitive code which happen to align vertically. Sublime takes that a step further by allowing for multiple selections. It is surprisingly useful for reformatting code, and as a replacement for Find-and-Replace. Just select (let&#8217;s say) the variable you want to rename, and hit Ctrl+D. The next occurrence of that word will get selected. Repeat as often as you like, the just start typing and you&#8217;ll be typing in multiple discrete locations. This feature alone has reduced the amount of macros and regexes I use to clean up text <i>drastically</i>. You&#8217;ll wonder how you lived without it, seriously.</p>
<p>The last thing I want to mention is the fledgling, but fast-growing developer community around Sublime. As a tool for programmers, its been designed to be extended with plugins, written in Python. What&#8217;s even better is that there&#8217;s a package repository plugin called <a href="https://github.com/wbond/sublime_package_control">Package Control</a>, written by <a href="https://github.com/wbond">Will Bond</a>, that completely streamlines the plugin installation and upgrade process integrated directly into the editor. Plugins are hosted on Github or Bitbucket and you can pull the upgrades as their published. After installing that package (which you can do just by pasting a command into the integrated python shell..!), all the available packages are shown in a command palette, and are installed without even requiring a restart. Since the code for the plugins is on Github, it&#8217;s really easy to fork it and send in pull requests if you find a bug, too. I highly recommend it!</p>
<p>And there&#8217;s just enough time for a quick little shout-out for one of my own plugins. If you&#8217;re writing Javascript with Sublime, you should totally install the <a href="https://github.com/spadgos/sublime-jsdocs">JSDocs</a> plugin. You can install it with the aforementioned Package Control plugin, or there&#8217;s instructions on the Github page. This plugin makes writing Javadoc-style comments a breeze by adding context-sensitive autocompletes, comment-aware linebreaking, and it even inspects your code to prefill @param and @return tags in a docblock. <a href="https://github.com/spadgos/sublime-jsdocs#readme">The readme file</a> has much more information and many examples. If you run into any problems, leave me a note in the <a href="https://github.com/spadgos/sublime-jsdocs/issues">issue tracker</a> and I&#8217;ll get right onto it!</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=69</wfw:commentRss>
		</item>
		<item>
		<title>Converting arguments into an array</title>
		<link>http://spadgos.com/?p=68</link>
		<comments>http://spadgos.com/?p=68#comments</comments>
		<pubDate>Wed, 21 Sep 2011 14:41:48 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=68</guid>
		<description><![CDATA[I've been working on the previously-mentioned Myrtle project, as well as its unit-testing sister project, Tyrtle (which will get further plugs soon...), and I kind of stumbled across this interesting little thing.
In javascript, there's a "magic" variable made available inside every function, called arguments. It appears to be an array at first glance, but it [...]]]></description>
			<content:encoded><![CDATA[<p>I've been working on the previously-mentioned <a href="http://www.github.com/spadgos/myrtle">Myrtle</a> project, as well as its unit-testing sister project, <a href="http://www.github.com/spadgos/tyrtle">Tyrtle</a> (which will get further plugs soon...), and I kind of stumbled across this interesting little thing.</p>
<p>In javascript, there's a "magic" variable made available inside every function, called arguments. It <strong>appears</strong> to be an array at first glance, but it isn't. Observe:</p>
<div class="syntax_hilite">
<div id="javascript-5">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> arr = <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'x'</span>, <span style="color: #3366CC;">'y'</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #009900; font-style: italic;">// a real array, for comparison</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">typeof</span> arguments; <span style="color: #009900; font-style: italic;">// &quot;object&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">typeof</span> arr;&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// &quot;object&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; arguments.<span style="color: #006600;">length</span>; <span style="color: #009900; font-style: italic;">// 2</span><br />
&nbsp; &nbsp; arr.<span style="color: #006600;">length</span>;&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// 2</span></p>
<p>&nbsp; &nbsp; arguments<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;&nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// &quot;x&quot;</span><br />
&nbsp; &nbsp; arr<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// &quot;x&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">typeof</span> arguments.<span style="color: #006600;">join</span>;&nbsp; <span style="color: #009900; font-style: italic;">// &quot;undefined&quot; !!!</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">typeof</span> arr.<span style="color: #006600;">join</span>;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// &quot;function&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// &quot;[object Arguments]&quot;&nbsp; !!!</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// &quot;[object Array]&quot;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
f<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'x'</span>, <span style="color: #3366CC;">'y'</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>It is actually a special type of object called Arguments, and it's documented well on the <a href="https://developer.mozilla.org/en/JavaScript/Reference/functions_and_function_scope/arguments">Mozilla Developer Network</a>. Essentially, it is just an enumerable object in that it has a .length property and other properties in the slots 0...length - 1, but it doesn't have Array as its prototype, and hence, it doesn't have any of the array functions on it.</p>
<p>Obviously, in most cases where you want to actually do something useful with the arguments object, you actually want to have an array, rather than this strange object. Converting it to an array is usually done like so (as is recommended in the MDN article linked above):</p>
<div class="syntax_hilite">
<div id="javascript-6">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> argsAsARealArray = Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>This works. If you really wanted to be pedantic about it, you could say that it isn't actually safe, in case some code overwrites Array.prototype.slice, but I digress. In any case, it's a lot of typing and can be very confusing to newbies.</p>
<p><em>Now, in the interests of openness, I originally wrote this post to talk about this amazing new technique I discovered: [].concat(arguments). I wrote benchmarks and everything. It was shorter and performed about 4x better on Firefox. Then I actually used it in my code and discovered <strong>that it doesn't even work</strong>. So, there you go. I thought I'd keep the bulk of this article and instead compare some other methods which actually do work...</em></p>
<p>I wrote some functions which convert their arguments to an array and then return it. The first two both use slice, but I wanted to see if there was a difference between creating an array literal or using the prototype (there isn't).</p>
<div class="syntax_hilite">
<div id="javascript-7">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> arrayProtoSlice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> arrayLiteralSlice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> splice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">splice</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>arguments, <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> push<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; x.<span style="color: #006600;">push</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>x, arguments<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> x;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> unshift<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; x.<span style="color: #006600;">unshift</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>x, arguments<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> x;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>The results were interesting. I tested by calling each of the above functions with 1000 arguments.</p>
<table width="100%">
<caption>Operations/second; the higher the better</caption>
<thead>
<tr>
<th>Browser</th>
<th>Slice</th>
<th>Splice</th>
<th>Push</th>
<th>Unshift</th>
</tr>
</thead>
<tbody>
<tr>
<th>Firefox 6.0.2</th>
<td>4044</td>
<td>3549</td>
<td>10068</td>
<td>9846</td>
</tr>
<tr>
<th>Chrome 13</th>
<td>37814</td>
<td>2701</td>
<td>38180</td>
<td>40952</td>
</tr>
</table>
<p>Yes, some of these numbers are bizarre, but they're not typos. Push/unshift is 250% faster than slice on Firefox, and only about 10% faster on Chrome. Yes, splice is  <strong>94% slower</strong> than any other method on Chrome -- even slower than Firefox in this test. Unshift out-performs Push on Chrome by about 8%, too.</p>
<p>Of course, the real benefit of [].slice.apply(arguments) is that it's a one-liner. In real life usage, at best, the push/unshift technique requires 2 lines, but could be extracted to an external function. Of course, adding another function call is not free, so I did another benchmark.</p>
<div class="syntax_hilite">
<div id="javascript-8">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> convertWithPush<span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; x.<span style="color: #006600;">push</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>x, args<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> x;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> pushWithFnCall <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> convertWithPush<span style="color: #66cc66;">&#40;</span>arguments<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">// and the same for unshift </span></div>
</div>
</div>
<p></p>
<p>In Chrome, the extra function call led to a ~33% slow down. It did not seem to affect Firefox's performance at all, however.</p>
<p>In summary:</p>
<ul>
<li>If you want a simple one liner, go with: var args = [].slice.apply(arguments)</li>
<li>If you want better performance, use: var args = []; args.push.apply(args, arguments)</li>
<li>Never use splice</li>
</ul>
<p>Please do run the tests yourself here: <a href="http://jsperf.com/converting-arguments-to-an-array">http://jsperf.com/converting-arguments-to-an-array</a> and here for <a href="http://jsperf.com/converting-arguments-to-an-array/2">version 2</a> which has the additional benchmarks. Let me know if I've forgotten a different method, or if I've stuffed up my tests somehow.</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=68</wfw:commentRss>
		</item>
		<item>
		<title>Introducing Myrtle: A Javascript Mocking Framework</title>
		<link>http://spadgos.com/?p=67</link>
		<comments>http://spadgos.com/?p=67#comments</comments>
		<pubDate>Wed, 10 Aug 2011 20:35:13 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=67</guid>
		<description><![CDATA[This is just a short post to mention a little project I just put together. I call it Myrtle (which I guess is some sort of play on words on the Mock Turtle), but in any case, it is a Javascript Mocking Framework!
The project code is available on Github, or you can just grab Myrtle.js [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a short post to mention a little project I just put together. I call it <em>Myrtle</em> (which I guess is some sort of play on words on the <a href="http://en.wikipedia.org/wiki/Mock_Turtle">Mock Turtle</a>), but in any case, it is a Javascript Mocking Framework!</p>
<p>The project code is available on <a href="https://github.com/spadgos/myrtle">Github</a>, or you can just grab <a href="https://raw.github.com/spadgos/myrtle/master/Myrtle.js">Myrtle.js</a> if you like.</p>
<p>So anyway, let's look at what it does. Let's say you're writing some tests for your code, and you want to check that a particular function is called. For example, you might have a Validator class which should be called when a form input is changed. Here's the signature of a basic validation class:</p>
<div class="syntax_hilite">
<div id="javascript-19">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> Validator = <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* @param {DOMElement} a HTML Input element<br />
&nbsp; &nbsp; &nbsp;* @param {String} a value to validate<br />
&nbsp; &nbsp; &nbsp;* @return {Boolean} If the value was valid.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; validate : <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>input, inputValue<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// code here</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>;</div>
</div>
</div>
<p></p>
<p>The first step is to <b>spy</b> on the validate method:</p>
<div class="syntax_hilite">
<div id="javascript-20">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> info = Myrtle.<span style="color: #006600;">spy</span><span style="color: #66cc66;">&#40;</span>Validator, <span style="color: #3366CC;">'validate'</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>Spying on a method modifies it so that metadata about that method is stored with each call to it, without changing the actual functionality. You can access this metadata through the API object returned by Myrtle.spy.</p>
<div class="syntax_hilite">
<div id="javascript-21">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> info = Myrtle.<span style="color: #006600;">spy</span><span style="color: #66cc66;">&#40;</span>Validator, <span style="color: #3366CC;">'validate'</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>$<span style="color: #66cc66;">&#40;</span>myInputElement<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a changed value'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">trigger</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'change'</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// let's check that validate was called</span><br />
info.<span style="color: #006600;">callCount</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// 1</span></p>
<p><span style="color: #009900; font-style: italic;">// and let's see what it returned</span><br />
info.<span style="color: #006600;">lastReturn</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// true</span></p>
<p><span style="color: #009900; font-style: italic;">// and let's check what the parameters passed to it were</span><br />
info.<span style="color: #006600;">lastArgs</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// [myInputElement, &quot;a changed value&quot;] </span></div>
</div>
</div>
<p></p>
<p>What's important to remember here is that the validate method is still executed as if nothing ever happened. Myrtle wouldn't be a good spy otherwise...</p>
<p>Other times you want to actually stop functions from running. For example, in a test environment, you might want to suppress error messages which you're triggering on purpose, or to stop AJAX functions from executing. In these cases, you can <b>stub</b> out a function.</p>
<div class="syntax_hilite">
<div id="javascript-22">
<div class="javascript">Myrtle.<span style="color: #006600;">stub</span><span style="color: #66cc66;">&#40;</span>Notifications, <span style="color: #3366CC;">'displayError'</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p>
This replaces the displayError method with a function which does nothing. Care should be taken in these cases that other code isn't expecting a return value from functions which you stub out completely.</p>
<p>You can also replace a function using stub. This is useful if you want to simulate a method, but not actually execute it - for example, when an AJAX method is called, you can put together a fake response.</p>
<div class="syntax_hilite">
<div id="javascript-23">
<div class="javascript">Myrtle.<span style="color: #006600;">stub</span><span style="color: #66cc66;">&#40;</span>Network, <span style="color: #3366CC;">'send'</span>, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>origFn, url, callback<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; callback<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>isSuccess : <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>You'll see there that the first parameter passed to the stub function is the original method. This is useful in cases where you may want to only stub it out based on some criteria, or to modify arguments or return values of the original method.</p>
<div class="syntax_hilite">
<div id="javascript-24">
<div class="javascript">Myrtle.<span style="color: #006600;">stub</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; Notifications,<br />
&nbsp; &nbsp; <span style="color: #3366CC;">'displayError'</span>,<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>origFn, message<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>message !== <span style="color: #3366CC;">'My expected error'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; origFn<span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>The last feature currently in Myrtle is some basic speed profiling. If you want to find out how fast your function is executed, or if it runs slower given some input, you can turn on profiling and find out.</p>
<div class="syntax_hilite">
<div id="javascript-25">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> info = Myrtle.<span style="color: #006600;">profile</span><span style="color: #66cc66;">&#40;</span>calc, <span style="color: #3366CC;">'factorial'</span><span style="color: #66cc66;">&#41;</span>;<br />
calc.<span style="color: #006600;">factorial</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#41;</span>;<br />
calc.<span style="color: #006600;">factorial</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#41;</span>;<br />
calc.<span style="color: #006600;">factorial</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">12</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>info.<span style="color: #006600;">getAverageTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// a number, like 12 (ms)</span><br />
info.<span style="color: #006600;">getQuickest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// { args : [3], ret : 6, time: 1 }</span><br />
info.<span style="color: #006600;">getSlowest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// { args: [12], ret: 479001600, time: 20 } </span></div>
</div>
</div>
<p></p>
<p>Those are the three main features of Myrtle (so far). However, the nice thing about them is that they can all be combined in any way. You can spy on and stub out a method, or spy and profile. Stubbing and profiling probably isn't so useful though, since you'd only be measuring the speed of performing your replacement method.</p>
<p>There are a few ways to combine the options, and it's important to know that each of the Myrtle functions returns the exact same API object per function. To demonstrate:</p>
<div class="syntax_hilite">
<div id="javascript-26">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> info1 = Myrtle.<span style="color: #006600;">spy</span><span style="color: #66cc66;">&#40;</span>Obj, <span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">// turn on spying</span><br />
<span style="color: #003366; font-weight: bold;">var</span> info2 = Myrtle.<span style="color: #006600;">stub</span><span style="color: #66cc66;">&#40;</span>Obj, <span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// turn on stubbing</span><br />
info1 === info2; <span style="color: #009900; font-style: italic;">// true </span></div>
</div>
</div>
<p>
There's also the Myrtle function itself which can be used:</p>
<div class="syntax_hilite">
<div id="javascript-27">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> info = Myrtle<span style="color: #66cc66;">&#40;</span>Obj, <span style="color: #3366CC;">'foo'</span>, <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; spy : <span style="color: #003366; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; stub : <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// my replacement</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; profile : <span style="color: #003366; font-weight: bold;">true</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>Of course, the last thing to cover is an important one: tidying up. Chances are that you don't want to stub out your methods for all the tests, and you want to restore them to how they were before at some point. Myrtle makes this super easy.</p>
<div class="syntax_hilite">
<div id="javascript-28">
<div class="javascript"><span style="color: #009900; font-style: italic;">// if you have a reference to the API object:</span><br />
info.<span style="color: #006600;">release</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// and even if you don't, remember that</span><br />
<span style="color: #009900; font-style: italic;">// Myrtle will give you it easily.</span><br />
Myrtle<span style="color: #66cc66;">&#40;</span>Obj, <span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">release</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// and if you're really lazy, just clean them all up!</span><br />
Myrtle.<span style="color: #006600;">releaseAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// if you're not sure if you've left</span><br />
<span style="color: #009900; font-style: italic;">// some hanging about, just check!</span><br />
Myrtle.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>So, like I said, the <a href="https://github.com/spadgos/myrtle">code is on Github</a>. Go have look and let me know what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=67</wfw:commentRss>
		</item>
		<item>
		<title>Image radio buttons</title>
		<link>http://spadgos.com/?p=66</link>
		<comments>http://spadgos.com/?p=66#comments</comments>
		<pubDate>Fri, 25 Jun 2010 06:41:13 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=66</guid>
		<description><![CDATA[I came across this Stack Overflow question today about replacing radio buttons with images, however the answers there didn't sit quite right with me. They were completely reliant on javascript, which was fine according to the OP, but I feel that javascript enhancements like this, especially when they deal with things which require user input, [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this Stack Overflow question today about <a href="http://stackoverflow.com/questions/3114166/replacing-radio-buttons-with-different-images">replacing radio buttons with images</a>, however the answers there didn't sit quite right with me. They were completely reliant on javascript, which was fine according to the OP, but I feel that javascript enhancements like this, especially when they deal with things which require user input, should be just that: <strong>enhancements</strong> and not the entire solution itself.</p>
<p><a href="http://www.alistapart.com/articles/progressiveenhancementwithjavascript/">Progressive enhancement</a> is a fairly well known concept. Start with a valid, semantic and, most importantly, <em>usable</em> page and then add the niceties to it with javascript. If javascript isn't available or something breaks, then you don't have the niceties but you still have a usable page. Like I said, this is especially important for user input. Nothing is more frustrating than not being able to use a form because some half-wit developer thought they'd do something clever with javascript... or so my users tell me, anyway.</p>
<p>So, getting back to the radio buttons. We should set up some requirements.</p>
<ul>
<li>Instead of radio buttons, we'd like to see an icon which has two states: on and off.</li>
<li>The icon behaves exactly like a radio button:
<ol>
<li>Clicking on it or its label activates it.</li>
<li>Clicking on a different button in the group deactivates it.</li>
<li>Accessible via the keyboard or mouse</li>
</ol>
</li>
<li>With javascript turned off, it should still work</li>
<li>Minimal extra markup required.</li>
<li>Cross browser</li>
</ul>
<p>The styles, including the icon images will be handled via CSS, so each radio button will need a class to identify which icon to use. We'll also need a class to identify which radio buttons should be replaced with images. This leaves our HTML fairly standard:</p>
<pre>
&lt;input type="radio" name="sex" value="male"
    class="image_radio male_icon" id="male_radio" />
&lt;label for="male_radio">Male&lt;/label>
&lt;input type="radio" name="sex" value="female"
    class="image_radio female_icon" checked="checked" />
</pre>
<p>I've not given the second radio button an ID or label, just to demonstrate that both are optional. I'm not much of a fan of the overuse of ID attributes...</p>
<p>Almost all the styling is done via CSS, of course:</p>
<pre>
label.image_radio {
    width: 16px;    /* the same size as our icons */
    height: 16px;
    display: inline-block;
}
label.image_radio input.image_radio {
    margin-left: -9999px;
    /* this effectively hides the radio button
    without removing it from the document flow */
}

/* define the icons for each radio */
label.male {          background-image: url(male_off.png);   }
label.male.active {   background-image: url(male_on.png);    }
label.female {        background-image: url(female_off.png); }
label.female.active { background-image: url(female_on.png);  }
</pre>
<p>So, if you were to take a look at this in your browser, you'd see a completely unstyled page with standard radio buttons. Excellent. Without the javascript, we haven't broken anything. Now, <a href="http://www.youtube.com/watch?v=Vxq9yj2pVWk">let's enhance</a>.</p>
<p>Before I jump into the jQuery, I'll just get on my soapbox for a quick second to note that if it weren't for IE, the following code would be a single statement (kinda). If you click on a label associated with an input, IE doesn't pass the click event on to the input so we have to manually add those labels into our result set before applying the click event.</p>
<div class="syntax_hilite">
<div id="php-30">
<div class="php">jQuery<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span>$<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get all the image radio buttons</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> <span style="color:#0000FF;">$radios</span> = $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'input.image_radio'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.php.net/each"><span style="color:#000066;">each</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> <span style="color:#0000FF;">$t</span> = $<span style="color:#006600; font-weight:bold;">&#40;</span>this<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// wrap each in a label, copying over all the classes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$t</span>.wrap<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"&lt;label&gt;&lt;/label&gt;"</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; className: <span style="color:#0000FF;">$t</span>.attr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'className'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toggleClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'active'</span>, <span style="color:#0000FF;">$t</span>.is<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">':checked'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// $clickTarget will be the elements to apply</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// the click event handler to</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color:#0000FF;">$clickTarget</span> = <span style="color:#0000FF;">$radios</span><br />
&nbsp; &nbsp; ;</p>
<p>&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>$.browser.msie<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get the parent labels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$clickTarget</span> = <span style="color:#0000FF;">$radios</span>.parent<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// now find all the labels which are for this element</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$radios</span>.<a href="http://www.php.net/each"><span style="color:#000066;">each</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>this.id<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$clickTarget</span> = <span style="color:#0000FF;">$clickTarget</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'label[for=&quot;'</span> + this.id + <span style="color:#FF0000;">'&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#0000FF;">$clickTarget</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .click<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">var</span> <span style="color:#0000FF;">$t</span> = $<span style="color:#006600; font-weight:bold;">&#40;</span>this<span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$label</span> = <span style="color:#0000FF;">$t</span>.closest<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'label'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<span style="color:#0000FF;">$label</span>.is<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">".active"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'label.active:has(:radio[name=&quot;'</span> + <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$t</span>.is<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">":radio"</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#0000FF;">$t</span> : <span style="color:#0000FF;">$t</span>.find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">":radio"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.attr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'name'</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#FF0000;">'&quot;])'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .removeClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'active'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$label</span>.addClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'active'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; ;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>And that's all there is to it. There's probably still quite a bit more work you could put into it (for example, though it's still completely accessible via the keyboard, there's no visual indication that a particular button has focus), but it provides nice progressive enhancement for your radio buttons. With some very small tweaks, the same code could also apply to checkboxes too!</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=66</wfw:commentRss>
		</item>
		<item>
		<title>Javascript default values</title>
		<link>http://spadgos.com/?p=65</link>
		<comments>http://spadgos.com/?p=65#comments</comments>
		<pubDate>Wed, 05 May 2010 06:36:48 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=65</guid>
		<description><![CDATA[Just a small post here to share a bit of benchmarking I just did. In Javascript, the boolean operators (&#124;&#124; and &#038;&#038;) behave a little differently to how many people expect. In PHP, the result of using &#124;&#124; or &#038;&#038; is always a boolean:


// PHP:
$x = "hello" &#124;&#124; false; // bool(true)
$x = false &#124;&#124; 0; [...]]]></description>
			<content:encoded><![CDATA[<p>Just a small post here to share a bit of benchmarking I just did. In Javascript, the boolean operators (|| and &#038;&) behave a little differently to how many people expect. In PHP, the result of using || or &#038;& is always a boolean:</p>
<div class="syntax_hilite">
<div id="php-35">
<div class="php"><span style="color:#FF9933; font-style:italic;">// PHP:</span><br />
<span style="color:#0000FF;">$x</span> = <span style="color:#FF0000;">"hello"</span> || <span style="color:#000000; font-weight:bold;">false</span>; <span style="color:#FF9933; font-style:italic;">// bool(true)</span><br />
<span style="color:#0000FF;">$x</span> = <span style="color:#000000; font-weight:bold;">false</span> || <span style="color:#CC66CC;">0</span>; <span style="color:#FF9933; font-style:italic;">// bool(false) </span></div>
</div>
</div>
<p>
Javascript, on the other hand, returns the <strong>first determining value in the statement</strong>.</p>
<div class="syntax_hilite">
<div id="php-36">
<div class="php"><span style="color:#FF9933; font-style:italic;">// Javascript:</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = <span style="color:#FF0000;">"hello"</span> || <span style="color:#000000; font-weight:bold;">false</span>; <span style="color:#FF9933; font-style:italic;">// string(&quot;hello&quot;)</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = <span style="color:#000000; font-weight:bold;">false</span> || <span style="color:#CC66CC;">0</span>; <span style="color:#FF9933; font-style:italic;">// int(0)</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = <span style="color:#CC66CC;">0</span> || <span style="color:#FF0000;">"hello"</span>; <span style="color:#FF9933; font-style:italic;">// string(&quot;hello&quot;)</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = <span style="color:#CC66CC;">5</span> &amp;&amp; <span style="color:#CC66CC;">2</span>; <span style="color:#FF9933; font-style:italic;">// int(2)</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = <span style="color:#CC66CC;">5</span> &amp;&amp; <span style="color:#FF0000;">""</span>; <span style="color:#FF9933; font-style:italic;">// string(&quot;&quot;)</span><br />
<span style="color:#000000; font-weight:bold;">var</span> x = NaN &amp;&amp; <span style="color:#CC66CC;">6</span>; <span style="color:#FF9933; font-style:italic;">// NaN </span></div>
</div>
</div>
<p>
That is, when using ||, the result will be the first truthy value or the last value.</p>
<p>When using &#038;&, the result will be the first falsey value or the last value.</p>
<p>"Truthy" and "falsey" here obviously means a value which, when cast to a boolean, is true or false, respectively. Falsey values are 0, -0, false, null, undefined, and NaN. Truthy values are everything else (including "0", empty arrays and empty objects).</p>
<p>Knowing this gives us the opportunity to use a handy little shortcut for setting some default values for variables.</p>
<div class="syntax_hilite">
<div id="php-37">
<div class="php"><span style="color:#000000; font-weight:bold;">function</span> foo<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; a = a || <span style="color:#FF0000;">"default a"</span>;<br />
&nbsp; &nbsp; alert<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;&nbsp; <span style="color:#FF9933; font-style:italic;">// &quot;default a&quot;</span><br />
foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// &quot;default a&quot;</span><br />
foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"my message"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// &quot;my message&quot; </span></div>
</div>
</div>
<p></p>
<p>The other important thing to know about the boolean operators is that they will <em>short-circuit</em> the processing once enough information is known to determine the output. That is, when using &#038;&, if the first parameter is falsey, then it doesn't matter at all what the second parameter is, since the output will always be false(y), so it skips evaluating that parameter. Similarly with ||, if the first parameter is truthy, then we can stop right there. This allows us to write a slightly more efficient default-value routine.</p>
<div class="syntax_hilite">
<div id="php-38">
<div class="php"><span style="color:#000000; font-weight:bold;">function</span> foo<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; a || <span style="color:#006600; font-weight:bold;">&#40;</span>a = <span style="color:#FF0000;">"default a"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; alert<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#FF9933; font-style:italic;">// this produces the same output as above </span></div>
</div>
</div>
<p>
If the first parameter is truthy, then it stops right there and never actually evaluates the second parameter which would assign a value to a. This is a bit more efficient than the first example, since there is only ever an assignment when it is required. How slight is this gain? Over 1,000,000 loops, running the first method took ~1640ms compared to ~1400ms for the second, so it's not going to make even a vaguely noticeable difference, but it doesn't hurt to do things the best way, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=65</wfw:commentRss>
		</item>
		<item>
		<title>Ubuntu, nooooo!</title>
		<link>http://spadgos.com/?p=64</link>
		<comments>http://spadgos.com/?p=64#comments</comments>
		<pubDate>Mon, 29 Mar 2010 00:06:04 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[ramblings]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=64</guid>
		<description><![CDATA[Starting with version 10.10, coming out in October this year, Ubuntu will be using base 10 units, taking its lead from Apple's move last year which I didn't much like. At least Ubuntu will give users an option to switch back, something Apple is yet to do. (Options? On a mac? ha!)
]]></description>
			<content:encoded><![CDATA[<p>Starting with version 10.10, coming out in October this year, <a href="http://www.neowin.net/news/ubuntu-implements-units-policy-will-switch-to-base-10-units-in-future-release">Ubuntu will be using base 10 units</a>, taking its lead from Apple's move last year which <a href="/?p=49">I didn't much like</a>. At least Ubuntu will give users an option to switch back, something Apple is yet to do. (Options? On a mac? ha!)</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=64</wfw:commentRss>
		</item>
		<item>
		<title>The great rep recalc of 2010</title>
		<link>http://spadgos.com/?p=63</link>
		<comments>http://spadgos.com/?p=63#comments</comments>
		<pubDate>Mon, 22 Mar 2010 06:44:01 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[ramblings]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=63</guid>
		<description><![CDATA[The Stack Overflow team recently announced that they were changing the question upvotes to only give 5 points instead of 10. 
Though I've got plenty more reputation than I could possibly need (that is, I have full user-moderator rights), this has affected my score. A lot. Like -4250. Bummer. I ask a lot of questions [...]]]></description>
			<content:encoded><![CDATA[<p>The Stack Overflow team recently announced that they were <a href="http://blog.stackoverflow.com/2010/03/important-reputation-rule-changes/">changing the question upvotes to only give 5 points instead of 10</a>. </p>
<p>Though I've got plenty more reputation than I could possibly need (that is, I have full user-moderator rights), this has affected my score. A lot. Like -4250. Bummer. <a href="http://spadgos.com/?p=58">I ask a lot of questions</a> and I think doing so is a really good thing, so I'm a little disappointed to see it go this way in discouraging new questions. Good questions already only attracted a fraction of the number of votes which good answers receive, so I guess I don't see it as fixing anything which was broken in the first place.</p>
<p>As you'd imagine, it stirred up a bit of malcontent in the comments thread (though, this happens with <em>any</em> change):</p>
<blockquote><p>This constant changing and making it all retroactive to all previous votes cast is crap. When people signed up you told them the rules and awarded points based on those rules.</p>
<p>I understand that things change and need to be changed in order to keep possible issues and problems in check. However rolling back people’s points is a jerk move and one you seem to have no issue doing.</p></blockquote>
<blockquote><p>I feel bad for the people [who have lost much rep], but we all get the added bonus of *having our questions answered* when we ask them. I think this change just shifts the focus of asking a question away from gaining reputation and towards getting an answer. Just as it should be.</p></blockquote>
<blockquote><p>I can understand the need of the changes *now*, but why punish those who helped bootstrap the system by providing lots of questions in the beginning?</p></blockquote>
<blockquote><p>Well, that’s demotivating. I’m pretty close to 10K, and this rule-change/recalc will drop me back to 8.7K.</p>
<p>Not much incentive to keep running on the rat machine, is it?</p></blockquote>
<p>I feel this guy's pain. Though it's obviously not the main reason I or anyone else would visit the site, trying to achieve a "high score" is one key feature, and knowing that it could be arbitrarily taken away from you just like that is quite demotivating. Though how can you argue when the site owner says this:</p>
<blockquote><p>I think it’s healthy for everyone to realize that rep is kind of an arbitrary concept, and think about what it means to them, to anyone else, and why.</p></blockquote>
<p>Overall, it seems as though not many are actually too concerned about the change in rep, but there's quite a lot of people who are <em>very</em> annoyed at the fact that it is being applied retroactively. There was a bit of a discussion/speculation about the notion that this was technically the only way to do it, which is rubbish. Yes, you don't want to have to keep a history of rules and the dates they changed, since that would grow unwieldy and awkward very quickly. Here's a smarter way to do it: get the Community User to double every single vote, then halve the score to +5. Done.</p>
<p>For a website built on community contribution, I'm just saddened to see how little they actually listened to their users. Perhaps only the voices of the top users were heard? The ones with <a href="http://spadgos.com/?p=58">nothing to lose anyway</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=63</wfw:commentRss>
		</item>
		<item>
		<title>A suggestion for Ford</title>
		<link>http://spadgos.com/?p=62</link>
		<comments>http://spadgos.com/?p=62#comments</comments>
		<pubDate>Sat, 16 Jan 2010 02:48:21 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[ramblings]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=62</guid>
		<description><![CDATA[Breaking with the theme of this blog, but this has been irritating me for some time now.
The "Voice Control" ad for the Ford Mondeo.





If you're going to make an ad touting a new feature such as voice control, it'd probably be a good idea to not highlight how buggy your software is.
]]></description>
			<content:encoded><![CDATA[<p>Breaking with the theme of this blog, but this has been irritating me for some time now.</p>
<p>The "Voice Control" ad for the Ford Mondeo.</p>
<object width="480" height="295">
<param name="movie" value="http://www.youtube.com/v/HgirOMNxlfM&#038;hl=en_GB&#038;fs=1&#038;"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<p><embed src="http://www.youtube.com/v/HgirOMNxlfM&#038;hl=en_GB&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object>
<p>If you're going to make an ad touting a new feature such as voice control, it'd probably be a good idea to not highlight how buggy your software is.</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=62</wfw:commentRss>
		</item>
		<item>
		<title>jQuery 1.4 released</title>
		<link>http://spadgos.com/?p=61</link>
		<comments>http://spadgos.com/?p=61#comments</comments>
		<pubDate>Thu, 14 Jan 2010 14:07:08 +0000</pubDate>
		<dc:creator>Fisher</dc:creator>
		
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://spadgos.com/?p=61</guid>
		<description><![CDATA[Despite the risk of joining in the echo chamber that I'm sure will start up very soon, I just wanted to write a little observation.
jQuery 1.4 has been released. Kinda.
It's a little baffling to me, considering the first release candidate was only announced one day ago, yet the dedicated jQuery 1.4 website proudly states "jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>Despite the risk of joining in the <a href="http://chris.pirillo.com/10-ways-to-eliminate-the-echo-chamber/">echo chamber</a> that I'm sure will start up very soon, I just wanted to write a little observation.</p>
<p>jQuery 1.4 has been released. Kinda.</p>
<p>It's a little baffling to me, considering the first release candidate was only announced <a href="http://jquery14.com/pre-release-2/jquery-14rc1">one day ago</a>, yet the dedicated <a href="http://www.jquery14.com">jQuery 1.4 website</a> proudly states <em>"jQuery 1.4 has been released!"</em>, but alas, there is no link. The <a href="http://code.jquery.com/jquery-nightly.js">nightly build</a> still reads "1.4a2-pre", so I guess it's up to me.</p>
<p><a href="http://code.jquery.com/jquery-1.4.js">jQuery 1.4 is here.</a></p>
<p>It's a tad overdue. The roadmap slated it for a November 2009 release, and it's been promised for a while now, but it's here now anyway. I was looking at a <a href="http://futurecolors.ru/jquery/">jQuery cheat sheet</a> which contains the new functions introduced in 1.4, and two of them jumped out at me: <a href="http://api.jquery.com/nextUntil/">nextUntil()</a> and <a href="http://api.jquery.com/prevUntil/">prevUntil</a>. They seem to be very similar to <a href="http://fisher.spadgos.com/jqAdjacent.html">something I've seen before</a>. Kinda. Maybe I'm just seeing things.</p>
]]></content:encoded>
			<wfw:commentRss>http://spadgos.com/?feed=rss2&amp;p=61</wfw:commentRss>
		</item>
	</channel>
</rss>

