Activate your free membership today | Log-in

Monday, October 13th, 2008

Ruby on jQuery and Closures

Category: jQuery, JSON

<p>Sam Ruby has that way about him that sees things very clearly. He just took a peak at jQuery for the first time and was able to really put into words what I think jQuery enthusiasts like about the library:

The notable thing about this is that despite all of the asynchronous events taking place, the code is sequential (nested, but sequential), and that the JSON results of the AJAX call is immediately available to the function that is invoked when the selection changes.

This is based on the following code that he wrote:

JAVASCRIPT:
  1.  
  2. $("#archive").click(function() {
  3.   $.getJSON('unscanned.cgi', {}, function(unscanned) {
  4.  
  5.     // replace realname input field with a selection list
  6.     var select = $('<select name="realname" id="realname"/>')[0];
  7.     for (var i=0; i<unscanned .length; i++) {
  8.       select.options[i] = new Option(unscanned[i][1], i);
  9.     }
  10.     $('#realname').before(select).remove();
  11.  
  12.     $("#archive").attr("disabled","disabled");
  13.  
  14.     // process selection
  15.     $('#realname').focus().change(function() {
  16.       var icla = unscanned[$("#realname option:selected").val()];
  17.       $("#realname").before('<input type="text" ' +
  18.         'id="realname" name="realname"/>').remove();
  19.       $("#realname").val(icla[1]);
  20.       $("#pubname").val(icla[2]);
  21.       $("#email").val(icla[3]);
  22.       $("#replaces").val(icla[0] + ':' + icla[3]);
  23.       $("#filename").val('').focus();
  24.       $("#archive").removeAttr("disabled");
  25.     });
  26.   });
  27.   return false;
  28. });
  29.  

Those that love jQuery, love the API.

John himself had an example in the comments:

JAVASCRIPT:
  1.  
  2. $(':radio[name=is_user]')
  3.   .change(function(){ $('.depends-isuser')[ this.value == 1 ? ‘show’ : ‘hide’ ](); })
  4.   .change();
  5.  

Related Content:

  • Ruby on Rails
    Ruby on Railsis an open source framework for Web development in Ruby, an object-oriented programming (OOP) language similar to Perl and Python. The...
  • jQuery finds important role in Ajax-style open source framework development
    The jQuery open source library and framework has gained greater attention among a slate of frameworks that includes Dojo, Prototype, GWT and others....
  • RHTML
    Ruby on Rails Script ( Ruby on Rails Project...
  • RBX
    Ruby on Rails Script ( Ruby on Rails Project...
  • ERB
    Ruby on Rails Script ( Ruby on Rails Project...

Posted by Dion Almaer at 6:40 am
1 Comment

++---
2.2 rating from 27 votes

1 Comment »

Comments feed TrackBack URI

have to admit although most of my work is in Prototype & Scriptaculous, jQuery is one of those things i love.

It’s so compact and fast, if you’re not after something big and weighty, you can build some excellent stuff in it (have replaced my rails blog with jquery for the small footprint alone).

Great stuff,

Comment by indiehead — October 13, 2008

Leave a comment

You must be logged in to post a comment.