<?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>scriptNode &#187; jquery</title>
	<atom:link href="http://scriptnode.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JSQL: JavaScript Query Language</title>
		<link>http://scriptnode.com/article/jsql-javascript-query-language/</link>
		<comments>http://scriptnode.com/article/jsql-javascript-query-language/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 06:12:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[script-sunday]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=286</guid>
		<description><![CDATA[About JSQL

When I started my new job, I had to make the transition from YUI to jQuery. I&#8217;d seen some jQuery code before (in fact, its terseness and use of $ as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the query part [...]]]></description>
			<content:encoded><![CDATA[<h3>About JSQL</h3>
<p>
When I started <a href="http://richter.paletteswap.com/why-i-left-yahoo-for-a-startup/">my new job</a>, I had to make the transition from <a href="http://developer.yahoo.com/yui/">YUI</a> to <a href="http://jquery.com/">jQuery</a>. I&#8217;d seen some jQuery code before (in fact, its terseness and use of <code>$</code> as both an object and a function were easy to spot), but I wasn&#8217;t familiar with it. I actually thought that the <em>query</em> part of jQuery meant there was some kind of <a href="http://mysql.com/">Standard Query Language</a> interface. There isn&#8217;t, but there is a really cool <a href="http://www.css3.info/">CSS3</a> selector instead.
</p>
<p>
Turns out, I was a little disappointed; I wanted to use something query-based. So, I started to throw this script together. I call it <acronym title="JavaScript Query Language">JSQL</acronym>. Below are some quick examples of what it looks like in action:
</p>
<p><script src="/assets/js/jsql/jsql-min.js" type="text/javascript"></script></p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var results = JSQL(&#8217;SELECT * FROM #ft WHERE nodeName = &#8220;LI&#8221;);<br />
</textarea></p>
<p>
JSQL always returns an object with an <code>error</code> property that&#8217;s either <code>Boolean false</code> or a <code>String</code> stating what error occurred (for example, &#8220;Unknown command UPDAT3&#8243;). If it successfully retrieves or operates on elements, it returns a <code>count:Number</code> property with the number of elements and an <code>elements:Array</code> property with those elements.
</p>
<p>
Here are some interactive examples:
</p>
<h3>Example 1: Checkboxes</h3>
<div class="section" id="jsql-example-1">
<div>
<input type="checkbox" value=""/> Option number 1
		</div>
<div>
<input type="checkbox" value=""/> Option number 2
		</div>
<div>
<input type="checkbox" value=""/> Option number 3
		</div>
</p></form>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = true WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1">Set Checked</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = true WHERE type = "checkbox"');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-1 SET checked = false WHERE type = &#8220;checkbox&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-1a">Uncheck</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-1a').onclick = function() {
JSQL('UPDATE #jsql-example-1 SET checked = false WHERE type = "checkbox"');
};
</script></p>
<h3>Example 2: Form Validation</h3>
<div class="section" id="jsql-example-2">
<p>
			First name:</p>
<input type="text" value=""/>
<p>
			Last name:</p>
<input type="text" value=""/>
<p>
			Email:</p>
<input type="text" value=""/>
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;error&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2">Highlight empty fields</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "error" WHERE value = ""');
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;UPDATE #jsql-example-2 SET parentNode.className = &#8220;&#8221; WHERE value = &#8220;&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-2a">Reset</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-2a').onclick = function() {
JSQL('UPDATE #jsql-example-2 SET parentNode.className = "" WHERE value = ""');
};
</script></p>
<h3>Example 3: Removing Elements</h3>
<div class="section" id="jsql-example-3">
<strong>Lorem</strong> Ipsum is simply dummy text of the <strong>printing and</strong> typesetting industry. <strong>Lorem</strong> Ipsum has been the <strong>industry&#8217;s</strong> standard dummy text <strong>ever</strong> since the 1500s, when an unknown printer took a galley of type and scrambled it to make a <strong>type specimen</strong> book. It has survived not only <strong>five</strong> centuries, but also the leap into <strong>electronic</strong> typesetting<strong>, remaining essentially unchanged</strong>. It was popularised in the 1960s with the release of <strong>Letraset</strong> sheets containing <strong>Lorem Ipsum</strong> passages, and more recently with <strong>desktop publishing</strong> software like Aldus PageMaker including versions of <strong>Lorem</strong> Ipsum.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8217;DELETE FROM #jsql-example-3 WHERE nodeName = &#8220;STRONG&#8221;&#8216;);<br />
</textarea></p>
<p>
<button id="jsql-button-3">Remove strong tags</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-3').onclick = function() {
JSQL('DELETE FROM #jsql-example-3 WHERE nodeName = "STRONG"');
};
</script></p>
<p>
In the spirit of jQuery, I also stuck in a quick <code>each</code> method which you can use to iterate through the returned elements immediately instead of setting up your own loop. The context is the current element (like jQuery) and the return value is still the same:</p>
<h3>Example 4: Selecting Elements</h3>
<div class="section" id="jsql-example-4">
Give him that <em>gun</em>. Give him <em>ALL</em> the guns.
</div>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8216;#ffff00&#8242;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4">Select &amp; Operate</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '#ffff00';
});
};
</script><br />
<textarea class="javascript" cols="50" name="code" rows="10"><br />
JSQL(&#8221;SELECT * FROM #jsql-example-4 WHERE nodeName = &#8216;EM&#8217;&#8221;).each(function() {<br />
	this.style.background = &#8221;;<br />
});<br />
</textarea></p>
<p>
<button id="jsql-button-4a">Undo</button>
</p>
<p><script type="text/javascript">
document.getElementById('jsql-button-4a').onclick = function() {
JSQL("SELECT * FROM #jsql-example-4 WHERE nodeName = 'EM'").each(function() {
	this.style.background = '';
});
};
</script></p>
<p>
I started coding this several months ago, and just recently decided to revisit it. I found the code hard to read myself when making some modifications before publishing it here. That typically means it&#8217;s poorly written. And I&#8217;m admittedly lousy at writing parsing code&#8230; so there&#8217;s that. I also doubt it&#8217;s realistic for any real-world websites (other libraries handle those jobs quite well), but I thought it would be fun to play with.
</p>
<p>
Some noticeable features lacking are equivalents to <code>ALTER</code> and <code>INSERT INTO</code>. I&#8217;ve also got it <a href="http://github.com/richtaur/jsql/tree/master">up on GitHub</a>, so feel free to fork it, and maybe add those methods yourself!
</p>
<ul>
<li><a href="/assets/js/jsql/jsql.js">[Download jsql.js]</a> (11k)</li>
<li><a href="/assets/js/jsql/jsql-min.js">[Download jsql-min.js]</a> (3,661 bytes)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/jsql-javascript-query-language/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>A Seemingly Snappier User Interface</title>
		<link>http://scriptnode.com/article/a-seemingly-snappier-user-interface/</link>
		<comments>http://scriptnode.com/article/a-seemingly-snappier-user-interface/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 04:55:20 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=263</guid>
		<description><![CDATA[

Say you&#8217;ve got a fancy user interface. You&#8217;re leveraging AJAX well, you&#8217;re making good use of best practices, and you&#8217;ve got fast servers that respond quickly.
But your user actions still aren&#8217;t quite as fast as you&#8217;d like. What else can you do?


If you&#8217;ve done things correctly, you&#8217;ve got a user interface that reacts positively to [...]]]></description>
			<content:encoded><![CDATA[<p><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script></p>
<p>
Say you&#8217;ve got a fancy user interface. You&#8217;re leveraging <a href="http://ajaxian.com">AJAX</a> well, you&#8217;re making good use of <a href="http://yuiblog.com/">best practices</a>, and you&#8217;ve got fast servers that <a href="/tag/optimization/">respond quickly</a>.<br />
But your user actions still aren&#8217;t quite as fast as you&#8217;d like. What else can you do?
</p>
<p>
If you&#8217;ve done things correctly, you&#8217;ve got a user interface that reacts positively to successful flows, but will also handle errors gracefully.<br />
Even though you&#8217;re &#8220;doing it right,&#8221; consider restructuring your application: <strong>assume success</strong>. Normally, your server is up and running and operations<br />
will be completely successfully. The edge case (hopefully) is the error case. Assuming this, we can create a slightly faster user interface.
</p>
<p>
Let&#8217;s begin with a basic voting mechanism. You&#8217;ve got a control on your page that lets a user vote on an object, and a small bit of text to represent the state of said control.<br />
It may look something like this:
</p>
<div class="section">
	<button>Vote</button><br />
	<span>You haven&#8217;t voted yet</span>
</div>
<p>
This example is meant to be easy and functional; hopefully <strong>your</strong> controls are prettier.<br />
Regardless, here&#8217;s a common flow for this request and its callback states:
</p>
<ol>
<li>Listen for the <code>click</code> event on the control.</li>
<li>Send the request.</li>
<li>Display a <a href="http://ajaxload.info/">&#8220;loading&#8221; message or a typical spinner image</a>.
<li>If the call comes back successfully, provide &#8220;success&#8221; feedback to the user and update the control.</li>
<li>If the call throws an error, provide an error message.</li>
</ol>
<p>
This flow is <strong>extremely</strong> common in today&#8217;s web. But we want something more immediately responsive, so let&#8217;s restructure the flow:
</p>
<ol>
<li>
		Use <code>onmousedown</code> because it will fire off just slightly sooner than <code>onclick</code> would.<br />
		(This might not suit all user interfaces but in this case it&#8217;s fine, and it wins us a few milliseconds.)
	</li>
<li>Provide &#8220;success&#8221; feedback to the user and update the control.</li>
<li>Send the request.</li>
<li>If the call throws an error, reset the controls and provide an error message.</li>
</ol>
<p>
This structure might seem a little off. Indeed, the error case even &#8220;lies&#8221; to the user for a few milliseconds if the request fails.<br />
But it covers all of its bases; none of the previous flow is omitted.<br />
Keep in mind that <strong>the error case is the edge case</strong>, and a very large majority of the time it&#8217;s fine to assume success at first.
</p>
<p>
Using <a href="http://jquery.com/">jQuery</a>, here&#8217;s what the code might look like:
</p>
<p><textarea class="javascript" cols="10" name="code" rows="10"><br />
// Attach the mousedown event listener<br />
$(&#8217;#snappier-1-btn&#8217;).mousedown(function(e) {</p>
<p>	var el = $(this);</p>
<p>	// Go ahead and assume things went smoothly<br />
	el.attr(&#8217;disabled&#8217;, true);<br />
	el.next().html(&#8217;Voted!&#8217;);</p>
<p>	// Send the XHR<br />
	$.ajax({<br />
		error : function() {</p>
<p>			// Undo the success message and allow the user to repeat<br />
			el.attr(&#8217;disabled&#8217;, false);<br />
			el.next().html(&#8221;You haven&#8217;t voted yet&#8221;);</p>
<p>			// Let the user know what happened<br />
			alert(&#8221;Sorry, an error occurred! Please try again.&#8221;);</p>
<p>		},<br />
		type : &#8216;GET&#8217;,<br />
		url : &#8216;/feed/&#8217;<br />
	});</p>
<p>});<br />
</textarea></p>
<p>
And here&#8217;s this flow working right here on the page:
</p>
<div class="section">
	<button id="snappier-1-btn">Vote</button><br />
	<span id="snappier-1-msg">You haven&#8217;t voted yet</span>
</div>
<p><script type="text/javascript"></p>
<p>// Attach the mousedown event listener
$('#snappier-1-btn').mousedown(function(e) {</p>
<p>	var el = $(this);</p>
<p>	// Go ahead and assume things went smoothly
	el.attr('disabled', true);
	el.next().html('Voted!');</p>
<p>	// Send the XHR
	$.ajax({
		error : function() {</p>
<p>			// Undo the success message and allow the user to repeat
			el.attr('disabled', false);
			el.next().html("You haven't voted yet");</p>
<p>			// Let the user know what happened
			alert("Sorry, an error occurred! Please try again.");</p>
<p>		},
		type : 'GET',
		url : '/feed/'
	});</p>
<p>});</p>
<p></script></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/a-seemingly-snappier-user-interface/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
