<?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>Belfry Images &#187; JavaScript</title>
	<atom:link href="http://blog.belfryimages.com.au/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.belfryimages.com.au</link>
	<description>Ben Scott's personal blog</description>
	<lastBuildDate>Fri, 20 Aug 2010 22:31:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Jeditable for inline editing</title>
		<link>http://blog.belfryimages.com.au/2010/02/04/using-jeditable-for-inline-editing/</link>
		<comments>http://blog.belfryimages.com.au/2010/02/04/using-jeditable-for-inline-editing/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 15:02:43 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=255</guid>
		<description><![CDATA[Using the Jeditable plugin for jQuery enables inline editing of a block of text. This makes it easy to take a static view and simply drop in editability. Say we start with the following HTML: &#60;label&#62;Name:&#60;/label&#62; &#60;span class=&#34;employeeName&#34;&#62;Ben Scott&#60;/span&#62;&#60;br/&#62; &#60;label&#62;Rating:&#60;/label&#62; &#60;span class=&#34;employeeRating&#34;&#62;Highly awesome&#60;/span&#62; We want to be able to edit the employeeName and employeeRating spans. [...]]]></description>
			<content:encoded><![CDATA[<p>Using the <a href="http://www.appelsiini.net/projects/jeditable">Jeditable</a> plugin for <a href="http://jquery.com/">jQuery</a> enables inline editing of a block of text. This makes it easy to take a static view and simply drop in editability. Say we start with the following HTML:</p>
<pre class="brush: html">
&lt;label&gt;Name:&lt;/label&gt; &lt;span class=&quot;employeeName&quot;&gt;Ben Scott&lt;/span&gt;&lt;br/&gt;
&lt;label&gt;Rating:&lt;/label&gt; &lt;span class=&quot;employeeRating&quot;&gt;Highly awesome&lt;/span&gt;
</pre>
<p>We want to be able to edit the <code>employeeName</code> and <code>employeeRating</code> spans. We need two actions (asssuming an MVC framework) to update the name and rating. The URLs might be something like <code>/employee/set_name/{id}</code> and <code>/employee/set_rating/{id}</code>. Each action should accept HTTP POST, take the new value in <code>$_REQUEST['new_value']</code> or similar, and return a HTTP status of 200 on success and 500 on failure, with the error message in the response. For example, using Slab (my PHP5 MVC framework, in development) the <code>set_name</code> action might be like this:</p>
<pre class="brush: php">
class EmployeeController extends AppController {
	function set_name($id) {
		$this-&gt;Employee-&gt;save(array(
			'id' =&gt; $id',
			'name' =&gt; $this-&gt;data['new_value']);
		));
		return $this-&gt;ajaxSuccess($this-&gt;data['new_value']);
	}
}
</pre>
<p>Slab catches uncaught exceptions and returns an AJAX failure with the exception message as the body of the response.</p>
<p>To hook up the fields to the actions, modify the HTML to include the URLs to the actions. While this means the markup has behavioural elements and isn&#8217;t purely presentational (and has a non-standard attribute), it makes the script a bit simpler and easy to move into a static .js file, and is a quick way to get a page working.</p>
<pre class="brush: html">
&lt;label&gt;Name:&lt;/label&gt; &lt;span class=&quot;employeeName&quot; editUrl=&quot;/employee/set_name/7&quot;&gt;Ben Scott&lt;/span&gt;&lt;br/&gt;
&lt;label&gt;Rating:&lt;/label&gt; &lt;span class=&quot;employeeRating&quot; editUrl=&quot;/employee/set_rating/7&quot;&gt;Highly awesome&lt;/span&gt;
</pre>
<p>Now for the script itself:</p>
<pre class="brush: javascript">
$(function(){
	$('.employeeName, .employeeRating').editable(
		function(value, settings) {
			return $.ajax({
				url: $(this).attr('editUrl'),
				data: { 'new_value': value },
				async: false,
				type: 'post'
			}).responseText;
		}, {
			indicator: 'Saving...',
			tooltip: 'Click to edit',
			onblur: 'submit'
		}
	);
});
</pre>
<p>The first argument is a function that returns the new value of the field. This is important when doing things like replacing line breaks with <code>&lt;br/&gt;</code> which we&#8217;re not worrying about, but it also gives us the ability to write our own AJAX code. By default Jeditable makes assumptions about how the update is done. The <code>async</code> option in the call to <code>$.ajax()</code> blocks until the call returns, and lets the function return the response of the AJAX call. The second argument are options to set some text to show while calling the update function, the tooltip to display when hovering over the span, and to submit changes on blurring the input which makes it seem a bit more usable when there are multiple fields.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2010/02/04/using-jeditable-for-inline-editing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cliche &#8211; an extremely simple view template engine for jQuery</title>
		<link>http://blog.belfryimages.com.au/2009/12/21/cliche-jquery-template-engine/</link>
		<comments>http://blog.belfryimages.com.au/2009/12/21/cliche-jquery-template-engine/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 12:25:14 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=210</guid>
		<description><![CDATA[I&#8217;ve written a very simple plugin for jQuery that takes a template and a model and returns a view of the model. To make sure it worked I wrote some tests and examples, and since I did that I figured I would write a little mini-site. The plugin itself is pretty straightforward: (function($){ $.fn.cliche = [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a very simple plugin for jQuery that takes a template and a model and returns a view of the model. To make sure it worked I wrote some tests and examples, and since I did that I figured I would write a little <a href="http://cliche.belfryimages.com.au">mini-site</a>. The plugin itself is pretty straightforward:</p>
<pre class="brush: javascript">
(function($){
	$.fn.cliche = function(model){
		var template = $(this).html();
		template = template.replace(/%7C/g, '|');	// pipes in a href like &lt;a href=&quot;|... get converted to %7C, convert it back
		template = template.replace(/\|.*?\|/g, function(f) {
			var val = model;
			var fieldParts = f.replace(/\|/g, '').split('.');
			for (var i = 0; i &lt; fieldParts.length; i ++) {
				val = val[fieldParts[i]];
			}
			return val;
		});
		return template;
	};
}(jQuery));
</pre>
<p>It is used by setting up a template (putting it into a <code>script type="text/html"</code> block is convenient and hides it from display) and calling <code>cliche(<em>model</em>)</code> on the jQuery object:</p>
<pre class="brush: html">
&lt;div id=&quot;output&quot;&gt;
	&lt;ul&gt;&lt;/ul&gt;
&lt;/div&gt;
&lt;script type=&quot;text/html&quot; id=&quot;testTemplate&quot;&gt;
	&lt;li id=&quot;item|id|&quot;&gt;&lt;a href=&quot;viewItem?id=|id|&quot;&gt;|name|&lt;/a&gt;&lt;/li&gt;
&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	$('#output ul').append(
		$('#testTemplate').cliche({
			id: 1, name: 'Lorem'
		})
	);
&lt;/script&gt;
</pre>
<p>Cliche is licensed under a <a href="http://creativecommons.org/licenses/by/2.5/au/">Creative Commons Attribution 2.5 Australia</a> license. Check it out at <a href="http://cliche.belfryimages.com.au">http://cliche.belfryimages.com.au</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2009/12/21/cliche-jquery-template-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RegEx Sandbox</title>
		<link>http://blog.belfryimages.com.au/2009/12/15/regex-sandbox/</link>
		<comments>http://blog.belfryimages.com.au/2009/12/15/regex-sandbox/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 12:19:08 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=207</guid>
		<description><![CDATA[I&#8217;ve just made a new tool live, it is a sandbox for playing with simple regular expressions in Javascript. I&#8217;ve used a couple of other web-based regex tools, what makes this one different is the result of the regular expression is shown live (as well as a couple of examples of what the regex should [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just made a new tool live, it is a sandbox for playing with simple regular expressions in Javascript. I&#8217;ve used a couple of other web-based regex tools, what makes this one different is the result of the regular expression is shown live (as well as a couple of examples of what the regex should look like in code). Try out the RegEx Sandbox at <a href="http://regex.belfryimages.com.au">regex.belfryimages.com.au</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2009/12/15/regex-sandbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Form validation with jQuery</title>
		<link>http://blog.belfryimages.com.au/2009/12/09/form-validation-with-jquery/</link>
		<comments>http://blog.belfryimages.com.au/2009/12/09/form-validation-with-jquery/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 09:40:50 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=204</guid>
		<description><![CDATA[Every time I&#8217;ve implemented client-side form validation I&#8217;ve started from scratch and done it a little differently. Usually it devolves into a messy set of if statements and duplicated code. Here&#8217;s my latest method, which separates the validation rules from the processing. It uses jQuery because if you&#8217;re not using a Javascript library you should [...]]]></description>
			<content:encoded><![CDATA[<p>Every time I&#8217;ve implemented client-side form validation I&#8217;ve started from scratch and done it a little differently. Usually it devolves into a messy set of <code>if</code> statements and duplicated code. Here&#8217;s my latest method, which separates the validation rules from the processing. It uses <a href="http://jquery.com">jQuery</a> because if you&#8217;re not using a Javascript library <a href="http://www.codinghorror.com/blog/archives/001275.html">you should be</a>. This will only handle relatively simple validation cases.</p>
<p>So start with a form:</p>
<pre class="brush: html">
	&lt;form id="mailingListSubscription" action="subscribe.php"&gt;
		Name: &lt;input type="text" name="name" id="name" /&gt;&lt;br /&gt;
		Email: &lt;input type="text" name="email" id="email" /&gt;&lt;br /&gt;
		Phone: &lt;input type="text" name="phone" id="phone" /&gt;&lt;br /&gt;
		&lt;button type="submit"&gt;Subscribe&lt;/button&gt;
	&lt;/form&gt;
</pre>
<p>All fields are required, and I&#8217;m going to use some magical regex (found on the interthingy somewhere) to validate the email address. This script sets up the rules:</p>
<pre class="brush: javascript">
	var rules = [
		{ id: 'name', test: function(val) { return val != ''; }, msg: 'Please enter your name' },
		{ id: 'email', test: function(val) { return val.search(/^[^@]+@[^@]+.[a-z]{2,}$/i) != -1; }, msg: 'Please enter a valid email address' },
		{ id: 'phone', test: function(val) { return val != ''; }, msg: 'Please enter your phone number' }
	];
</pre>
<p>Each rule has the id of the form element being tested, a message that gets displayed on failing the rule, and a function that validates the value of the form element. I also could add multiple rules for the one input.</p>
<p>This script sets up the <code>submit</code> handler for the form, which does the validation using the array of rules set up above:</p>
<pre class="brush: javascript">
	$(function(){
		$('form#mailingListSubscription').submit(function(){
			for (var i = 0; i &lt; rules.length; i ++) {
				var rule = rules[i];
				var target = $('#'+rule.id);
				if (!rule.test(target.val())) {
					alert(rule.msg);
					target.focus();
					return false;
				}
			}
			return true;
		});
	});
</pre>
<p>On a test failing, the rule&#8217;s <code>msg</code> value is shown and the target of the test gets focus. This could be changed to something more user friendly like showing the message next to the target field.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2009/12/09/form-validation-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deleting items in a table with AJAX via JQuery</title>
		<link>http://blog.belfryimages.com.au/2009/11/29/deleting-items-in-a-table-with-ajax-via-jquery/</link>
		<comments>http://blog.belfryimages.com.au/2009/11/29/deleting-items-in-a-table-with-ajax-via-jquery/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 05:04:42 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=197</guid>
		<description><![CDATA[This is a very straight-forward tutorial on implementing a jQuery-driven &#8216;delete via AJAX&#8217; feature. Say we have a plain HTML table containing a list of items and a &#8216;Remove&#8217; link. I&#8217;m not going to describe the back-end, but I&#8217;m assuming something groovy like CakePHP or ASP.NET MVC. I&#8217;ve also assumed that the delete request always [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very straight-forward tutorial on implementing a jQuery-driven &#8216;delete via AJAX&#8217; feature. Say we have a plain HTML table containing a list of items and a &#8216;Remove&#8217; link. I&#8217;m not going to describe the back-end, but I&#8217;m assuming something groovy like <a href="http://cakephp.org/">CakePHP</a> or <a href="http://www.asp.net/mvc/">ASP.NET MVC</a>. I&#8217;ve also assumed that the delete request always succeeds and never returns an error, which may not be the case. The script itself is a more than required but is my preferred method as I can extend the elements in the UI fairly easily.</p>
<pre class="brush: html">
	&lt;table&gt;
		&lt;tr&gt;&lt;td&gt;Chickpeas&lt;/td&gt;      &lt;td&gt;&lt;a href="/items/delete/1" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;Garlic&lt;/td&gt;         &lt;td&gt;&lt;a href="/items/delete/2" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;Olive oil&lt;/td&gt;      &lt;td&gt;&lt;a href="/items/delete/3" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;Tahini&lt;/td&gt;         &lt;td&gt;&lt;a href="/items/delete/4" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;Cumin&lt;/td&gt;          &lt;td&gt;&lt;a href="/items/delete/5" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;Lemon juice&lt;/td&gt;    &lt;td&gt;&lt;a href="/items/delete/6" class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
	&lt;/table&gt;
</pre>
<p>The delete hrefs (<code>/items/delete/XX</code>) link to an action or page that deletes the specified item and returns a HTTP status of 200 (OK). If the action just redirected to the current page then this table should work as it stands, which is probably a good way to check that everthing works as expected without involving AJAX features. If you just want to set up the client side without implementing any server-side code, create the following in delete_test.php and use it for the delete links:</p>
<pre class="brush: php">
&lt;?php header('HTTP/1.1 200 OK'); ?&gt;
</pre>
<p>Make sure that jQuery 1.3+ has been included in the page and add the following:</p>
<pre class="brush: javascript">
&lt;script type="text/javascript"&gt;
$(function(){
	var ui = {
		init: function(){
			$('a.delete').live('click', ui.delete_click);
		},

		delete_click: function(){
			link = this;
			$.get(link.href, function(data, status) {
				$(link).parents('tr').remove();
			});
			return false;
		}
	};

	ui.init();
});
&lt;/script&gt;
</pre>
<p>Very basic stuff but it works. It could be jazzed up by fading out the items first or updating a status label. If there is a significant delay between calling the delete action and getting a response the user may not think anything has happened, so perhaps the delete link should change or be disabled.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2009/11/29/deleting-items-in-a-table-with-ajax-via-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Blink plugin</title>
		<link>http://blog.belfryimages.com.au/2009/10/08/jquery-blink-plugin/</link>
		<comments>http://blog.belfryimages.com.au/2009/10/08/jquery-blink-plugin/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 11:10:05 +0000</pubDate>
		<dc:creator>ben</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.belfryimages.com.au/?p=167</guid>
		<description><![CDATA[I love this Blink plugin by John Boker. Not because it&#8217;s something that should ever be used (I don&#8217;t think that was the intent). It&#8217;s just a good, simple reference plugin.]]></description>
			<content:encoded><![CDATA[<p>I love <a href="http://www.antiyes.com/jquery-blink-plugin">this Blink plugin by John Boker</a>. Not because it&#8217;s something that should ever be used (I don&#8217;t think that was the intent). It&#8217;s just a good, simple reference plugin.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.belfryimages.com.au/2009/10/08/jquery-blink-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
