<?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>Rails on the Run &#187; javascript</title>
	<atom:link href="http://railsontherun.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsontherun.com</link>
	<description>Rails experiments by Matt Aimonetti</description>
	<lastBuildDate>Tue, 23 Feb 2010 07:28:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>keeping my javascript files organized</title>
		<link>http://railsontherun.com/2007/12/05/keeping-my-javascript-files-organized/</link>
		<comments>http://railsontherun.com/2007/12/05/keeping-my-javascript-files-organized/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 04:01:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[content_for]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[low pro]]></category>
		<category><![CDATA[lowpro]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[structure]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/12/05/keeping-my-javascript-files-organized</guid>
		<description><![CDATA[I was recently asked by Rubyist friend (Josh Knowles) how I was organizing my Javascript files when using LowPro.
LowPro is the best solution for doing Unobtrusive Javascript using Prototype.
With the help of LowPro, you define behaviors that get triggered by the user. This is great, however, you&#8217;ll notice that some behaviors are used all over [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked by Rubyist friend (<a href="http://joshknowles.com/">Josh Knowles</a>) how I was organizing my Javascript files when using <a href="http://lowpro.stikipad.com/home/show/HomePage">LowPro</a>.</p>
<p><a href="http://lowpro.stikipad.com/home/show/HomePage">LowPro</a> is the best solution for doing <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">Unobtrusive Javascript</a> using <a href="http://www.prototypejs.org/">Prototype</a>.</p>
<p>With the help of <a href="http://lowpro.stikipad.com/home/show/HomePage">LowPro</a>, you define behaviors that get triggered by the user. This is great, however, you&#8217;ll notice that some behaviors are used all over the place (a date chooser for instance) and some complicated behaviors only get used on very specific pages.</p>
<p>First things first, let&#8217;s look at the header in my application.html.erb file (located in app/views/layouts).<br />
This is the default layout used by all my views, I rarely use more than 5 layouts per app and always use a default layout.</p>
<p>Please note that I&#8217;m using Rails 2.0 so some features you&#8217;ll see in my file won&#8217;t work in Rails 1.2.x. (if you want to know about all the new Rails sexiness, check on <a href="http://peepcode.com/products/rails2-pdf">this awesome Peepcode PDF</a>.</p>
<p>Here we go:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;%= javascript_include_tag <span class="s"><span class="dl">'</span><span class="k">prototype</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k">effects</span><span class="dl">'</span></span>, <span class="sy">:cache</span> =&gt; <span class="pc">true</span> <span class="s"><span class="dl">%&gt;</span><span class="k"><tt>
</tt>  &lt;%= javascript_include_tag 'lowpro', :cache =</span><span class="dl">&gt;</span></span> <span class="pc">true</span> <span class="s"><span class="dl">%&gt;</span><span class="k"><tt>
</tt>  &lt;%= javascript_include_tag  'application', :cache =</span><span class="dl">&gt;</span></span> <span class="pc">true</span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>The first thing you might notice is that I don&#8217;t use</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;%= javascript_include_tag <span class="sy">:defaults</span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>The reason? I don&#8217;t want to load prototype.js, effects.js, controls.js, dragdrop.js, and application.js all at once. I almost never use drag&#8217;n'drop and seldom use Autocompleter and InPlaceEditor so why loading them in each and every single page of my apps? I&#8217;m not saying they are bad libraries, I&#8217;m just saying that in more than 80% of my page, I don&#8217;t use them, so they should not be in my default page load.</p>
<p>The second thing you might notice, I use :cache => true. Asset caching is a new feature in Rails 2.0 which combines related assets into a single file (works with css and js and only in production mode)<br />
 Note that the above code is untested, but everything should be loaded properly, otherwise, make sure proto gets loaded first, then lowpro, then application. (and you can probably create a one-liner)</p>
<p>All the default behaviors are defined in the application.js file, so they get loaded on all page. However to handle action specific behaviors, I use another Rails trick right in the header:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"> &lt;%= <span class="r">yield</span> <span class="sy">:javascript</span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>Why? Very simple, I want to load some custom JS in the header depending on the action that is used. For instance, when a visitor goes to my fancy ajax photo editor, I want to load the content editor javascript right in the header where it belongs.</p>
<p>For that, I simply need to add the following to my view:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;% content_for <span class="sy">:javascript</span> <span class="r">do</span> <span class="s"><span class="dl">%&gt;</span><span class="k"><tt>
</tt>    &lt;%= javascript_include_tag &quot;photos_show&quot; %</span><span class="dl">&gt;</span></span><tt>
</tt>  &lt;% <span class="r">end</span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>If content_for :javascript isn&#8217;t define, noting is yield in my header and therefore nothing is included but whenever I need, I can access my header directly from the view and insert javascript code in a very clean way.</p>
<p>photos_show.js is the javascript defining all the behaviors related to the photos controller and the show action. I usually only have few actions with a lot of custom behaviors so, these structure works well for me.</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  javascripts<tt>
</tt>    <span class="er">\</span>controller_action.js<tt>
</tt>    <span class="er">\</span>controller_another_action.js<tt>
</tt>    <span class="er">\</span>another_controller_action.js<tt>
</tt>    ...</pre>
</td>
</tr>
</table>
<p>However in the case of an app with a lot of custom behaviors, I recommend using the following structure:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  javascripts<tt>
</tt>    <span class="er">\</span> controller_name_<tt>
</tt>            <span class="er">\</span>action_name_.js<tt>
</tt>      <span class="er">\</span>another_controller_name<tt>
</tt>              <span class="er">\</span>action_name_.js<tt>
</tt>  ...</pre>
</td>
</tr>
</table>
<p>That&#8217;s it folks, I&#8217;m not a javascript expert, and if you know better, don&#8217;t hesitate to leave me a comment. What I know for sure, is that since I started using LowPro and behavior driven with Prototype, I have much more fun. Adding a bit of structure is a simple way for me to keep my code clear and help other people who have to work with me. (more on that later)</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/12/05/keeping-my-javascript-files-organized/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>LowPro trunk, what&#8217;s new?</title>
		<link>http://railsontherun.com/2007/11/27/lowpro-0-5-what-s-new/</link>
		<comments>http://railsontherun.com/2007/11/27/lowpro-0-5-what-s-new/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 06:44:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lowpro]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[san diego]]></category>
		<category><![CDATA[ujs]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/11/27/lowpro-0-5-what-s-new</guid>
		<description><![CDATA[If you&#8217;ve read my post on Ajax pagination you know that I&#8217;m a big fan of Dan Webb&#8217;s LowPro unobtrusive javascript library.
Doing Unobtrusive Javascript (UJS) is basically registering event handlers programmatically using CSS selectors to select the elements to register. In other words : keeping things separate and avoiding inline javascript.
If you&#8217;ve been using LowPro [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve read my <a href="http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes">post on Ajax pagination</a> you know that I&#8217;m a big fan of <a href="http://www.danwebb.net">Dan Webb</a>&#8217;s <a href="http://www.danwebb.net/lowpro">LowPro</a> unobtrusive javascript library.</p>
<p>Doing Unobtrusive Javascript (UJS) is basically registering event handlers programmatically using CSS selectors to select the elements to register. In other words : keeping things separate and avoiding inline javascript.</p>
<p>If you&#8217;ve been using LowPro 0.4 and recently tried to upgrade to <a href="http://www.prototypejs.org/">Prototype 1.6</a> you probably noticed that things don&#8217;t work as they used to. </p>
<p>The first thing you want to do, is to update to the <a href="http://svn.danwebb.net/external/lowpro/trunk/dist/">latest version of Lowpro</a>.</p>
<h2>So what&#8217;s new in 0.5 trunk?</h2>
<p>First off, you need to know that a lot of lowpro features were moved in Prototype 1.6 core <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li>You now get a warning via firebug if you try to use Low Pro with a version of Prototype that its not designed to work with.</li>
<li>Alternative event system ripped out: uses core events</li>
<li>DOM method mixins ripped out: alternatives all in prototype</li>
<li>Event.onReady delegates to the new dom:loaded event.  However this doesn&#8217;t fire immediately if the dom is already loaded like Event.onReady did. (might be patched to work as before)</li>
<li>DOMBuilder is staying but now is a thin shell around the new proto 1.6 Element stuff.</li>
<li>You can still return false from event handlers in addEvent and Behaviors to stop the event but now if you use Event.observe raw you don&#8217;t get this.</li>
<li>Behavior.create now works like Class.create in 1.6 so behaviors can have full inheritance:</li>
</ul>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="co">Basic</span> = <span class="co">Behavior</span>.create({<tt>
</tt>   onclick: function() {<tt>
</tt>    alert(<span class="s"><span class="dl">'</span><span class="k">woo</span><span class="dl">'</span></span>);<tt>
</tt>   }<tt>
</tt>  });<tt>
</tt><tt>
</tt>  <span class="co">SuppedUp</span> = <span class="co">Behavior</span>.create({<tt>
</tt>   onclick: function(<span class="gv">$super</span>) {<tt>
</tt>    alert(<span class="s"><span class="dl">'</span><span class="k">wee</span><span class="dl">'</span></span>);<tt>
</tt>    <span class="gv">$super</span>();<tt>
</tt>   }<tt>
</tt>  });</pre>
</td>
</tr>
</table>
<p>Works really nicely.</p>
<ul>
<li>
<p>core behaviors : Remote and Observed are now moved into the lowpro core (you don&#8217;t need to include the external files).</p>
</li>
<li>
<p>Event.addBehavior.reassignAfterAjax defaults to false. If you want re assign behaviors after an ajax call, you need to turn this option to true.</p>
</li>
<li>
<p>Event.addBehavior.reload(); added to reload/re assign behaviors. Very useful if you dynamically insert elements you want to observe!</p>
</li>
<li>
<p><a href="http://lowprojs.com">new website</a> has been set up and will contain documentation and tips &#8211; Full API docs coming soon.  There&#8217;s also a dedicated <a href="http://groups.google.co.uk/group/low-pro">google group</a>.  </p>
</li>
</ul>
<p>Here is a quick example with real life code. (which could be refactored, I know)</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt><strong>15</strong><tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt><strong>25</strong><tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt>29<tt>
</tt><strong>30</strong><tt>
</tt>31<tt>
</tt>32<tt>
</tt>33<tt>
</tt>34<tt>
</tt><strong>35</strong><tt>
</tt>36<tt>
</tt>37<tt>
</tt>38<tt>
</tt>39<tt>
</tt><strong>40</strong><tt>
</tt>41<tt>
</tt>42<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c">// Make sure the behaviors still work even after navigating to another page using the ajax navigation.</span><tt>
</tt>Event.addBehavior.reassignAfterAjax = <span class="pc">true</span>;<tt>
</tt><tt>
</tt><span class="c">// Behaviors</span><tt>
</tt>Event.addBehavior({<tt>
</tt>  <span class="c">// Pagination links  </span><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">div.pagination a</span><span class="dl">'</span></span> : Remote.<span class="pt">Link</span>,<tt>
</tt><tt>
</tt>  <span class="c">// Reset the list when a user clicks on cancel.</span><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">a.cancel_button:click</span><span class="dl">'</span></span> : <span class="r">function</span>() {<tt>
</tt>    $(<span class="s"><span class="dl">'</span><span class="k">list_of_things</span><span class="dl">'</span></span>).update(<span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span>);<tt>
</tt>  },<tt>
</tt><tt>
</tt>  <span class="c">// carousel navigation prev</span><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">a#carousel_prev:click</span><span class="dl">'</span></span> : <span class="r">function</span>() {<tt>
</tt>    moveCarousel(<span class="s"><span class="dl">'</span><span class="k">prev</span><span class="dl">'</span></span>); <span class="r">return</span> <span class="pc">false</span>;<tt>
</tt>  },<tt>
</tt><tt>
</tt>  <span class="c">// carousel navigation next</span><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">a#carousel_next:click</span><span class="dl">'</span></span> : <span class="r">function</span>() {<tt>
</tt>    moveCarousel(<span class="s"><span class="dl">'</span><span class="k">next</span><span class="dl">'</span></span>); <span class="r">return</span> <span class="pc">false</span>;<tt>
</tt>  },<tt>
</tt><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">div.panel_pic:click</span><span class="dl">'</span></span> : <span class="r">function</span>() {<tt>
</tt>    removePanelPic(<span class="pc">this</span>);<tt>
</tt>  },<tt>
</tt><tt>
</tt>  <span class="s"><span class="dl">'</span><span class="k">div.photo_from_row img:click</span><span class="dl">'</span></span> : <span class="r">function</span>() {<tt>
</tt>    <span class="c">// Get the div holding the pic and use it as a target</span><tt>
</tt>    <span class="r">var</span> target = <span class="pc">this</span>.up();<tt>
</tt>    addPicToPanel(target);<tt>
</tt>    new Effect.Highlight(target);<tt>
</tt>  }<tt>
</tt>});<tt>
</tt><tt>
</tt><span class="r">function</span> addPicToPanel(target){<tt>
</tt>  new Insertion.Bottom(<span class="s"><span class="dl">'</span><span class="k">control_panel_photos</span><span class="dl">'</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">&lt;div id='edit_</span><span class="dl">&quot;</span></span>+ target.id +<span class="s"><span class="dl">&quot;</span><span class="k">' class='panel_pic'&gt;&lt;img class='panel_pic' src='</span><span class="dl">&quot;</span></span> +  target.immediateDescendants()[0].src + <span class="s"><span class="dl">&quot;</span><span class="k">'/&gt;&lt;/div&gt;</span><span class="dl">&quot;</span></span>);<tt>
</tt>  <span class="c">// Reload the behaviors so the new inserted pic can be monitored </span><tt>
</tt>  <span class="c">// and the 'div.panel_pic:click' behavior can be triggered</span><tt>
</tt>  Event.addBehavior.<span class="fu">reload</span>();<tt>
</tt>}<tt>
</tt>  </pre>
</td>
</tr>
</table>
<p>LowPro is a great way of keeping your code really clean and your views very accessible.</p>
<p>If you are interested in knowing more about UJS, come to our <a href="http://sdruby.com/">SDruby group meeting</a> on Dec 6 @ 7:30pm (<a href="http://tinyurl.com/2f486e">directions</a>). And if you don&#8217;t care about UJS, come later to hear about Facebook API. Don&#8217;t forger to bring your questions for our first <a href="http://groups.google.com/group/sdruby/browse_thread/thread/d488b70d67f84a5f#">Rails roundtable</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/11/27/lowpro-0-5-what-s-new/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax Pagination in less than 5 minutes</title>
		<link>http://railsontherun.com/2007/09/27/ajax-pagination-in-less-than-5-minutes/</link>
		<comments>http://railsontherun.com/2007/09/27/ajax-pagination-in-less-than-5-minutes/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 03:40:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[behaviors]]></category>
		<category><![CDATA[graceful degradation]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lowpro]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[progressive enhancement]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ujs]]></category>
		<category><![CDATA[unobtrusive]]></category>
		<category><![CDATA[will_paginate]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2008/02/28/ajax-pagination-in-less-than-5-minutes</guid>
		<description><![CDATA[updated Nov 26 to reflect the recent low pro changes. (please use low pro 0.5 and Prototype 1.6)
Recently one of my client asked me to add &#8216;ajax&#8217; pagination to his application. His site already had a very nice pagination using the excellent will_paginate from Mislav and the guys(PJ &#38; Chris) from err the blog but [...]]]></description>
			<content:encoded><![CDATA[<h2>updated Nov 26 to reflect the recent low pro changes. (please use low pro 0.5 and Prototype 1.6)</h2>
<p>Recently one of my client asked me to add &#8216;ajax&#8217; pagination to his application. His site already had a very nice pagination using the excellent <a href="http://plugins.require.errtheblog.com/browser/will_paginate">will_paginate</a> from <a href="http://www.workingwithrails.com/person/2764-mislav-marohni">Mislav</a> and the guys(PJ &amp; Chris) from <a href="http://errtheblog.com/">err the blog</a> but since my client had a special need where he had to have Ajax.</p>
<p>It took me <strong>virtually no time to convert the standard pagination into an Ajax navigation</strong> while still degrading gracefully.(it works even without Javascript)</p>
<p>I really enjoy using will_paginate, it&#8217;s very well written and the authors keep up with the <a href="http://err.lighthouseapp.com/projects/466-plugins/tickets?q=tagged%3Awill_paginate">bugs</a> and <a href="http://err.lighthouseapp.com/projects/466-plugins/tickets?q=tagged%3Awill_paginate">new features</a>.</p>
<h2>Start by installing will_paginate:</h2>
<pre><code>ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate
</code></pre>
<h2>Then go watch the <a href="http://railscasts.com/episodes/51">Railcast screencast</a> about will paginate.</h2>
<p>Once you have your pagination working, we will do some &#8216;<a href="http://en.wikipedia.org/wiki/Progressive_enhancement">progressive enhancement</a>&#8216;.</p>
<p>What we want is to <strong>add a behavior to the pagination link</strong>. The behavior would make the same call than the normal link but via an ajax(Javascript) call. </p>
<h2>Add lowpro</h2>
<p>To do that, you simply need to add the excellent <a href="http://www.danwebb.net/lowpro">&#8216;lowpro&#8217;</a> Prototype extension from <a href="http://www.danwebb.net">Dan Webb</a></p>
<p>You can get the files directly from the <a href="http://svn.danwebb.net/external/lowpro/trunk/dist/lowpro.js">lowpro&#8217;s repository</a>.</p>
<p>Add <a href="http://svn.danwebb.net/external/lowpro/trunk/dist/lowpro.js">lowpro.js</a> to your public/javascript folder.</p>
<p>Don&#8217;t forget to include the javascript in your page. (</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><tt>
</tt>  &lt;%= javascript_include_tag <span class="s"><span class="dl">'</span><span class="k">lowpro</span><span class="dl">'</span></span> <span class="s"><span class="dl">%&gt;</span><span class="k"><tt>
</tt></span></span></pre>
</td>
</tr>
</table>
<h2>Create a behavior</h2>
<p>Now open your application.js file (or whichever Javascript file you&#8217;re using) and add the following:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><tt>
</tt>  <span class="co">Event</span>.addBehavior.reassignAfterAjax = <span class="pc">true</span>;<tt>
</tt>  <span class="co">Event</span>.addBehavior({<tt>
</tt>    <span class="s"><span class="dl">'</span><span class="k">div.pagination a</span><span class="dl">'</span></span> : <span class="co">Remote</span>.<span class="co">Link</span><tt>
</tt>  })<tt>
</tt></pre>
</td>
</tr>
</table>
<p>Refresh your cache, reload your page, and test the link. It will probably look like it doesn&#8217;t do anything but if you are using <a href="http://www.getfirebug.com/">firebug</a> or if you are checking your logs, you&#8217;ll notice something happened. The problem is that we didn&#8217;t tell our action to send an Ajax response so we get the html full page all over again.</p>
<h2>Setup a response for javascript requests</h2>
<p>Got to your action handling the pagination and let&#8217;s setup a response for Javascript:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><tt>
</tt>  <span class="r">def</span> <span class="fu">index</span><tt>
</tt>  <span class="iv">@photos</span> = <span class="co">Photo</span>.paginate(<span class="sy">:all</span>, <span class="sy">:conditions</span> =&gt; [<span class="s"><span class="dl">&quot;</span><span class="k">photos.user_id = ?</span><span class="dl">&quot;</span></span>, current_user.id], <span class="sy">:page</span> =&gt; params[<span class="sy">:page</span>])<tt>
</tt>    respond_to <span class="r">do</span> |format|<tt>
</tt>      format.html <span class="c"># index.html.erb</span><tt>
</tt>      format.js <span class="r">do</span><tt>
</tt>        render <span class="sy">:update</span> <span class="r">do</span> |page|<tt>
</tt>          page.replace_html <span class="s"><span class="dl">'</span><span class="k">photos</span><span class="dl">'</span></span>, <span class="sy">:partial</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">photos</span><span class="dl">&quot;</span></span><tt>
</tt>        <span class="r">end</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt></pre>
</td>
</tr>
</table>
<p>Perfect! Now when a visitor clicks on my pagination links, only the photos are paginated, the rest of the page stays the same. Note that my navigation bar is inside the partial so it gets &#8216;updated&#8217; after a visitor clicks on any pagination link.</p>
<h2>Read more and convert to UJS (unobtrusive javascript)</h2>
<p>Read more about <a href="http://jlaine.net/2007/8/3/from-rails-ajax-helpers-to-low-pro-part-i">UJS</a> and think about replacing all your nasty inline javascript snippets by pretty behaviors <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (Think about stopping using the obtrusive rails helpers)</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/09/27/ajax-pagination-in-less-than-5-minutes/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>

