<?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>gaskell.org &#187; jQuery</title>
	<atom:link href="http://gaskell.org/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://gaskell.org</link>
	<description>picking up where teh rest leave off</description>
	<lastBuildDate>Sun, 15 Nov 2009 07:55:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unobtrusive deleting with Rails and jQuery</title>
		<link>http://gaskell.org/unobtrusive-deleting-with-rails-and-jquery/</link>
		<comments>http://gaskell.org/unobtrusive-deleting-with-rails-and-jquery/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 07:33:18 +0000</pubDate>
		<dc:creator>andy gaskell</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://gaskell.org/?p=194</guid>
		<description><![CDATA[I&#8217;m going to cover creating a sample Rails application, taking a look at how destroy works by default and how we can improve accessibility by writing unobtrusive JavaScript using the jQuery library.
First let&#8217;s create a new Rails application. We&#8217;ll call it ajaxstore.

Now let&#8217;s create some scaffolding and take a look at how it works.

Launch your [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to cover creating a sample Rails application, taking a look at how destroy works by default and how we can improve accessibility by writing unobtrusive JavaScript using the jQuery library.</p>
<p>First let&#8217;s create a new Rails application. We&#8217;ll call it ajaxstore.</p>
<p><script src="http://gist.github.com/234977.js"></script></p>
<p>Now let&#8217;s create some scaffolding and take a look at how it works.</p>
<p><script src="http://gist.github.com/234978.js"></script></p>
<p>Launch your favorite toob surfer to <a href="http://localhost:3000/products">http://localhost:3000/products</a> and create a few products to use. Back at the <a href="http://localhost:3000/products">products list</a> let&#8217;s take a look at the Destroy links. From a user&#8217;s perspective if you have JavaScript enabled you&#8217;ll get a confirmation dialog. If the user has JavaScript disabled he will be brought to the show action, and probably feel a little lost. From a developer standpoint there&#8217;s a lot of unnecessary repeated JavaScript on the Destroy links. Let&#8217;s get this fixed.</p>
<p>Let&#8217;s remove the default JavaScript that Rails adds to our project since we&#8217;re going to use jQuery.</p>
<p><script src="http://gist.github.com/234979.js"></script></p>
<p>Now let&#8217;s download jQuery. </p>
<p><script src="http://gist.github.com/234980.js"></script></p>
<p>Open up routes.rb &#8211; we need to add an option to the products resource route. I&#8217;m using delete as my option. You could overload destroy but in my opinion that makes the destroy action less cohesive.</p>
<p><script src="http://gist.github.com/234983.js"></script></p>
<p>We now need to add a view for our new action. Create a new file named &#8216;delete.html.erb&#8217; in your product views directory. We&#8217;ll keep it fairly simple, and ask the user to confirm the destroy.</p>
<p><script src="http://gist.github.com/235046.js"></script></p>
<p>At this point we need to do some refactoring. Open up the Products controller and look at all the places we’re calling Product.find(params[:id]). We’re going to refactor that into a <a href="http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html">before filter</a> and simplify the controller a bit. Here’s the entire controller.</p>
<p><script src="http://gist.github.com/235051.js"></script></p>
<p>Here we removed the edit and show actions, removed the first line from the update and destroy actions, created a method to load the product, and finally setup the filter. Now lets change how we generate our destroy links in the index and show views. First open up the Product index view and change the destroy link (should be around line 15) to this.</p>
<p><script src="http://gist.github.com/235052.js"></script></p>
<p>We&#8217;ll use the delete class in our JavaScript to handle delete clicks. For the show view, just remove the destroy link altogether. Browse to <a href="http://localhost:3000/products">http://localhost:3000/products</a> and create a product if need be, and now if you click on the &#8220;Delete&#8221; link we are brought to our delete view and our non-JavaScripting friends want to give us high fives. For the rest of our users, navigating to a new page is a bit much for deleting a product. Open up the layout file and add this under the stylesheet_link_tag.</p>
<p><script src="http://gist.github.com/235055.js"></script></p>
<p>We&#8217;re creating a AUTH_TOKEN variable so that when we make a POST, PUT or DELETE via ajax we can pass along the <a href="http://guides.rubyonrails.org/security.html#csrf-countermeasures">authentication token</a>. Let’s add some jQuery code to application.js to handle deletions. This JavaScript has been pulled from a larger application so some parts of are maybe a little too industrial strength for a blog post, but I’m going to focus on the Rails side and less on the jQuery. If there’s enough demand to go into details on the jQuery we’ll talk more about it &#8211; the main points are the ajaxSend handler and the a.delete click handler.</p>
<p><script src="http://gist.github.com/235056.js"></script></p>
<p>We also need to make some changes to our CSS file – here are the styles we need to add.</p>
<p><script src="http://gist.github.com/235061.js"></script></p>
<p>Let’s get Rails setup to do some ajax&#8217;n. There are a couple changes you need to make to every application. Add these lines under the protect_from_forgery line in your application controller.</p>
<p><script src="http://gist.github.com/235065.js"></script></p>
<p>And under those lines add the corresponding methods:</p>
<p><script src="http://gist.github.com/235067.js"></script></p>
<p>The set_xhr_flash method removes the flash if the current request is an ajax request. If we don&#8217;t clear the flash on an ajax request the next time the user makes a full http request the user will see the flash message. The other method is for safari and IE browsers – we&#8217;re correcting some headers for ajax requests. One last change to our destroy action in our Products controller and we’re good to go.</p>
<p><script src="http://gist.github.com/235070.js"></script></p>
<p>Start up your browser and server if need be. We are now fully web 2.0. JavaScripters and NonJavaScripters can use our app equally and that makes us totally rad web folks. You can <a href="http://github.com/agaskell/ajaxstore/">download the full project over at github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gaskell.org/unobtrusive-deleting-with-rails-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determining if a checkbox is checked with jQuery</title>
		<link>http://gaskell.org/determining-if-a-checkbox-is-checked-with-jquery/</link>
		<comments>http://gaskell.org/determining-if-a-checkbox-is-checked-with-jquery/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 23:07:32 +0000</pubDate>
		<dc:creator>andy gaskell</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://gaskell.org/?p=148</guid>
		<description><![CDATA[This one comes up quite often on the Stack Overflow, so I&#8217;ll give my shot at answering.
If you have a single checkbox that you&#8217;d like to check I like this style:

    if($('#your_checkbox_id').is(':checked')) {
      alert('checked!');
    } else {
      alert('not checked!');
 [...]]]></description>
			<content:encoded><![CDATA[<p>This one comes up quite often on the <a href="http://stackoverflow.com/search?q=jquery+checkbox">Stack Overflow</a>, so I&#8217;ll give my shot at answering.</p>
<p>If you have a single checkbox that you&#8217;d like to check I like this style:</p>
<pre>
<code>    if($('#your_checkbox_id').is(':checked')) {
      alert('checked!');
    } else {
      alert('not checked!');
    }</code>
</pre>
<p>Here&#8217;s another way of determining of a checkbox is checked:</p>
<pre>
<code>    if($('#your_checkbox_id:checked').length) {
      alert('checked!');
    } else {
      alert('not checked!');
    }</code>
</pre>
<p>Here&#8217;s more jQuery checkbox selector goodness.</p>
<p>This selector will get all of the checkboxes (checked or unchecked) in the DOM:</p>
<pre>
<code>    $(':checkbox');</code>
</pre>
<p>All of the checked checkboxes:</p>
<pre>
<code>    $(':checkbox:checked');</code>
</pre>
<p>All of the unchecked checkboxes:</p>
<pre>
<code>    $(':checkbox').not(':checked');</code>
</pre>
<p>Here&#8217;s all of the checkboxes within a form:</p>
<pre>
<code>    $('form :checkbox');</code>
</pre>
<p>How to check all checkboxes:</p>
<pre>
<code>    $(':checkbox').attr('checked', 'checked');</code>
</pre>
<p>Unchecking all checkboxes:</p>
<pre>
<code>    $(':checkbox').removeAttr('checked');</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://gaskell.org/determining-if-a-checkbox-is-checked-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery plugins and the importance of learning JavaScript basics</title>
		<link>http://gaskell.org/jquery-plugins-and-the-importance-of-learning-javascript-basics/</link>
		<comments>http://gaskell.org/jquery-plugins-and-the-importance-of-learning-javascript-basics/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 01:15:18 +0000</pubDate>
		<dc:creator>andy gaskell</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://gaskell.org/?p=126</guid>
		<description><![CDATA[With jQuery increasing in popularity choosing to use jQuery in your application is probably a smart move. However if you&#8217;re new to jQuery and more importantly new to JavaScript you need to learn the basics. jQuery makes writing plugins ridiculously easy and even easier to search for.  Before you run off to the googs [...]]]></description>
			<content:encoded><![CDATA[<p>With jQuery <a href="http://www.google.com/trends?q=jquery%2C+Prototype+Javascript%2C+mootools%2C+dojo&#038;ctab=0&#038;geo=all&#038;date=ytd&#038;sort=0">increasing in popularity</a> choosing to use jQuery in your application is probably a smart move. However if you&#8217;re new to jQuery and more importantly new to JavaScript you need to learn the basics. jQuery makes writing plugins ridiculously easy and even easier to <a href="http://www.google.com/#hl=en&#038;q=jquery+plugin+sum">search for</a>.  Before you run off to the googs in search of a plugin to add numbers together, take the time to learn the very basics of JavaScript. </p>
<p><img src="http://gaskell.org/wp-content/uploads/2009/08/little_one.jpg" alt="Little One is disappointed in your lack of JavaScript basics!" title="Little One is disappointed in your lack of JavaScript basics!" width="270" height="360" class="size-full wp-image-129" style="float:left;margin-right:10px;" /></p>
<p>Following the <a href="http://www.w3schools.com/jsref/default.asp">w3schools tutorial</a> and glancing over the <a href="http://www.w3schools.com/JS/default.asp">reference</a> of built in functions is going to help a lot when starting out with jQuery. With a solid foundation of JavaScript the next thing to learn is how <a href="http://docs.jquery.com/Selectors">selectors</a> work. Armed with JavaScript and selector knowledge most problems you come across probably will not need plugins. Plugins do have their place &#8211; if your application is Form intensive, by all means use the <a href="http://malsup.com/jquery/form/">Forms plugin</a> &#8211; just don&#8217;t have your entire client side experience be a bunch of plugins kludged together.</p>
]]></content:encoded>
			<wfw:commentRss>http://gaskell.org/jquery-plugins-and-the-importance-of-learning-javascript-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bulletproof Ajax with ASP.NET MVC (this time with jQuery)</title>
		<link>http://gaskell.org/bulletproof-ajax-with-aspnet-mvc-this-time-with-jquery/</link>
		<comments>http://gaskell.org/bulletproof-ajax-with-aspnet-mvc-this-time-with-jquery/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 22:11:11 +0000</pubDate>
		<dc:creator>andy gaskell</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://gaskell.org/?p=81</guid>
		<description><![CDATA[As a follow up a recent post about using unobtrusive javascript and Ajax in your ASP.NET MVC applications, I ported the Bulletproof Ajax bookstore application to use jQuery instead of Hijax. This led to more terse code &#8211; lines of javascript were reduced from 178 to 41. If you&#8217;ve spent at least few hours jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow up a <a href="/bulletproof-ajax-with-aspnet-mvc/">recent post</a> about using <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript" target="_blank">unobtrusive javascript</a> and Ajax in your ASP.NET MVC applications, I ported the <a href="http://www.amazon.com/Bulletproof-Ajax-Jeremy-Keith/dp/0321472667&amp;tag=9182-20">Bulletproof Ajax</a> bookstore application to use jQuery instead of <a href="http://bulletproofajax.com/shop/javascript/hijax.js">Hijax</a>. This led to more terse code &#8211; lines of javascript were reduced from 178 to 41. If you&#8217;ve spent at least few hours jQuery then you should find that the new code is also more readable. No server side code changes were necessary.
<p>jQuery is also really easy to extend &#8211; I wrote a couple of extensions that would be usable throughout an application. Here&#8217;s the extension I wrote to produce the ubiquitous <a href="http://www.37signals.com/svn/archives/000558.php">yellow fade effect</a>: </p>
<pre class="code">(<span style="color: blue">function</span>($) {
   $.fn.yellowFade = <span style="color: blue">function</span>() {
    <span style="color: blue">return this</span>.css({backgroundColor: <span style="color: #a31515">&quot;#ffffcc&quot;</span>})
    .animate({backgroundColor: <span style="color: #a31515">&quot;#ffffff&quot;</span>}, 1500, <span style="color: #a31515">&quot;linear&quot;</span>);
   }
 })(jQuery);</pre>
<p>Calling this method flows with the rest of jQuery style chaining of methods:</p>
<pre class="code"><span style="color: blue">function</span>(data) { $(<span style="color: #a31515">&quot;div#basket&quot;</span>).html(data).yellowFade(); });</pre>
<p>This one liner is a callback from our http post to the /Cart/AddProduct. The data parameter contains the response from the server. $(<span style="color: #a31515">&quot;div#basket&quot;</span>) is getting the container div from our shopping cart. Then we replace the html within the shopping cart div with the response from the server and follow it up with a yellow fade to provide user feedback.</p>
<p>Code is downloadable <a href="http://gaskell.org/upload/jShop.zip" target="_blank">here</a>. The <a href="http://jshop.gaskell.org" target="_blank">demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gaskell.org/bulletproof-ajax-with-aspnet-mvc-this-time-with-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.233 seconds -->
