<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : Tag ajax, everything about ajax</title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>Refactoring asynchronous code</title>
      <description>&lt;p&gt;How to refactor a long chunk of asynchronous code is one thing I learned during my Javascript &amp; CouchDB project. It&amp;#8217;s not a difficult thing, but I thought it was before I figured out how to do it, so I guess it might be interesting for Javascript newbies.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Asynchronous code&lt;/h3&gt;


&lt;p&gt;In the first couple of weeks I struggled with this new style of coding before I got the hang of it. If you&amp;#8217;re fairly new to Javascript, you&amp;#8217;re probably used to just assign a variable in one line and use it in the next line. In the asynchronous world you query for a value and do stuff with it &lt;i&gt;in the callback function&lt;/i&gt; of the query.&lt;/p&gt;


&lt;p&gt;To illustrate - &amp;#8220;normal&amp;#8221; code looks like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; result = db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select * from T&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="c"&gt;// use result&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Although writing web applications just screams for doing it like this instead:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select..&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt; (result) {
&lt;span class="c"&gt;// use result&lt;/span&gt;
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p style="font-size:0.8em"&gt;(Examples stolen from &lt;a href="http://nodejs.org/"&gt;Ryan Dahl&amp;#8217;s Presentation about Node.js&lt;/a&gt;.)&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;The Problem&lt;/h3&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-bad.png" alt="Bad asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;I have a method that checks if I have to display a notification message. For this it makes a lot of requests to the database, the received data has to be compared with other data, there&amp;#8217;s lots of if/else clauses to consider all kinds of conditions and edge cases. It started simple, but then more and more conditions came in, unless I had the &lt;a href="http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0128766398f7970c-pi"&gt;much feared diagonal closing bracket line&lt;/a&gt; on my screen. &lt;/p&gt;


&lt;p&gt;You don&amp;#8217;t need to know the details, just look at the picture and you&amp;#8217;ll get the impression.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;clear:left"&gt;Refactoring&lt;/h3&gt;


&lt;p&gt;By way of illustration I made up a much shorter example:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar == whatever){
      &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
      openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
        &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
          &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
        }
      });
    }
  });
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;It is time to refactor after you are sure this actually is the way to go and the code basically works (&amp;#8220;make it work, then make it pretty&amp;#8221;). First you group the code into methods: You split the lines by what they do, and then you name each section. I think around 8 LOC is a good length for a method. &lt;/p&gt;


&lt;p&gt;If this was synchronous code, you could just call these methods after each other. But this doesn&amp;#8217;t work here, because &lt;i&gt;result&lt;/i&gt; and &lt;i&gt;result2&lt;/i&gt; are not available outside the function that retrieves them. &lt;/p&gt;


&lt;p&gt;My first approach was to call the functions from within each other. This works - but to see what &lt;i&gt;checkForConflicts&lt;/i&gt; does at its very heart, you have to scroll down through all the methods - not much gained:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;);
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      doSomethingElse(result);
    }
  });
}

doSomethingElse(result){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      showMessage(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The way to do it is to use &lt;i&gt;callbacks&lt;/i&gt;. Each method gets an anonymous function as (additional) argument: in the method declaration this function has the name &lt;i&gt;callback&lt;/i&gt;. This callback is called if all the conditions within the method are met. The callback gets its arguments from within the method that calls it. You have to specify these arguments again in &lt;i&gt;checkForConflicts&lt;/i&gt; to have a valid function definition.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    doSomethingElse(result, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
      showMessage(result2); 
    });
  });
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, callback){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      callback(result);
    }
  });
}

doSomethingElse(result, callback){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      callback(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;When you look at &lt;i&gt;checkForConflicts&lt;/i&gt;, you now immediately see what it does, without being bothered by the details. As a side effect, deciding which code needs access to which arguments helps you to understand your code better. In my case I was able to optimize the use of passed variables a lot.&lt;/p&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-good.png" alt="Refactored asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;You might argue that the code is much longer now. First, longer is not worse if it is more readable. Second, this is only an example - if there was code instead of &lt;i&gt;//do stuff&lt;/i&gt; the few extra lines wouldn&amp;#8217;t carry weight.&lt;/p&gt;


&lt;p&gt;Finally here is a screenshot of my refactored code. All the methods don&amp;#8217;t fit on one screen, but you can see what &lt;i&gt;checkForConflicts&lt;/i&gt; does very quickly.&lt;/p&gt;


&lt;p style="clear:left"/&gt;
</description>
      <pubDate>Mon, 15 Feb 2010 16:17:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e9117d9-b37a-467b-99ac-b6f035ed1a9e</guid>
      <comments>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code#comments</comments>
      <category>javascript</category>
      <category>refactoring</category>
      <category>ajax</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=11</trackback:ping>
      <link>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code</link>
    </item>
    <item>
      <title>Testing PUT requests with Culerity</title>
      <description>&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="/files/Image/Bildschirmfoto%202010-02-02%20um%2012.38.41.png" alt="All green!" /&gt;&lt;/p&gt;


&lt;p&gt;One major pain at testing with culerity so far was a HtmlUnit bug that causes a ScriptException with PUT AJAX requests in jQuery. When you tried to test an update you got a &amp;#8220;java.lang.IllegalArgumentException: The content cannot be null&amp;#8221;. Fortunately, this bug is fixed now. With latest HtmlUnit build culerity should run fine, and I tell you how.&lt;/p&gt;


&lt;p&gt;I assume you have culerity installed already. If not, follow &lt;a href="http://github.com/langalex/culerity"&gt;these instructions&lt;/a&gt; for testing Rails apps &lt;a href="http://upstre.am/2009/10/25/testing-couchapps-with-cucumber-and-culerity/"&gt;or these&lt;/a&gt; for testing Couchapps.&lt;/p&gt;


&lt;p&gt;1. Make sure you have up-to-date versions of celerity and culerity:&lt;br/&gt;
$ jruby -S gem install celerity #(gives you celerity-0.7.8)&lt;br/&gt;
$ gem install culerity &amp;#8211;source http://gemcutter.org #(gives you culerity-0.2.7)&lt;/p&gt;


&lt;p&gt;2. Get the latest HtmlUnit nightly build:&lt;br/&gt;
$ wget http://build.canoo.com/htmlunit/artifacts/htmlunit-2.7-SNAPSHOT-with-dependencies.zip&lt;br/&gt;
$ unzip htmlunit-2.7-SNAPSHOT-with-dependencies.zip &lt;br/&gt;
$ cd htmlunit-2.7-SNAPSHOT-with-dependencies/lib&lt;br/&gt;
These are the jar files you need to copy into your celerity gem.&lt;/p&gt;


&lt;p&gt;3. Find your celerity gem and copy the files:&lt;br/&gt;
$ jruby -S gem which celerity&lt;br/&gt;
$ cp * PATH_OF_JRUBY_INSTALLATION/lib/ruby/gems/1.8/gems/celerity-0.7.7/lib/celerity/htmlunit&lt;/p&gt;


&lt;p&gt;4. The new HtmlUnit needs JQuery 1.4, so you &lt;a href="http://docs.jquery.com/Downloading_jQuery#Current_Release"&gt;need to upgrade&lt;/a&gt; if you haven&amp;#8217;t done already. There&amp;#8217;s a &lt;a href="http://github.com/jquery/jquery-compat-1.3"&gt;backwards compatibility plugin for 1.3&lt;/a&gt; if this gives you trouble.
&lt;/p&gt;


&lt;p&gt;Have fun!&lt;/p&gt;
</description>
      <pubDate>Sun, 31 Jan 2010 21:22:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:40dcb9ab-9661-4888-bdb1-d9ac36fb7fc2</guid>
      <comments>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity#comments</comments>
      <category>testing</category>
      <category>culerity</category>
      <category>ajax</category>
      <category>htmlunit</category>
      <category>bug</category>
      <category>celerity</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=10</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity</link>
    </item>
  </channel>
</rss>
