jMaki on Rails


Yes, it's me again. I really am alive, but have been heads down learning new technologies. You see, my latest role is as architect around Web 2.0 (or next generation web) development as it relates to Sun's tools. That being said, I have watched with interest the rapidly improving JRuby[1] implementation of the Ruby language, and growing market success of the Ruby on Rails[2] framework. It was time to learn a little bit of Ruby (along with other dynamic languages), and a larger bit of Rails, to understand what was going on outside the world of languages that begin with "J" and end with "a". After all, I have been preaching about the wider world inside the halls of Sun ... it was time to get down and dirty with some real code.

Initially, I was a bit of a skeptic, being very happy in my "strongly typed" comfort zone. Yet, I discovered what many other Java developers have discovered about Rails -- it really can make developing web applications more fun. More on that in the future.

jMaki On Rails

During the same time frame, I started following the jMaki[3] project (and its spinoff widget libraries project[4]) more closely. It became clear that the conceptual approaches taken by jMaki and Rails were very much compatible with each other. For example, here's an RHTML view containing a Yahoo Menu widget:

You can see that jMaki is represented as a helper method, just like all the built in helpers provided by Rails. The first argument is the name of the widget you want to have created, and the remaining arguments are used to compose a hash of options that are used to configure that widget. This corresponds to the use of JavaScript object literals in the other server side languages supported by Rails.

Of course, you can pass information from your Rails controller to jMaki, just like you can with all the other helper methods, via instance variables created by your action methods. For example, if you had built up a hash named @model_data that contained all the information currently included literally in the view above, you would end up with something much simpler:

  <%= jmaki_widget "", @menu_data %>

Hybrid approaches, with data partially included in the view and partially provided by the model, are available as well. A common use case for this would be in a table or data grid control, where you might specify the column headings in the view and pull the row data from the model. Any combination is possible, given that the second and subsequent arguments are all just Ruby expressions.

Using jMaki On Rails

Support for jMaki is still pretty bleeding edge, but will make its way towards a fully functional release in the next few months. If you want to play with it today, start by grabbing either Ruby or JRuby, and also installing Rails (1.2.x or later) and a Subversion client. The jMaki support is provided as a set of Rails plugins ... one for the core runtime, and one for each widget library you want to support.

Let's create a Rails application, and then load jMaki support for both Yahoo and Spry widgets. In this case I am adding the jMaki plugins repository to my local configuration -- that is optional, but makes the install commands easier to perform.

  rails jmaki_demo
  cd jmaki_demo
  script/plugin source
  script/plugin install jmaki_core
  script/plugin install jmaki_spry
  script/plugin install jmaki_yahoo

At this point, my Rails application is fully jMaki-enabled. In the <head> section every page, we will need to establish the jMaki environment for that page (typically, you will do this in a layout to avoid repeating yourself):

       <%= stylesheet_link_tag "jmaki-standard", :media => "all" -%>
       <%= javascript_include_tag "jmaki" -%>
       <%= jmaki_setup -%>

Finally, individual jMaki widgets are created by using the jmaki_widget helper method, which takes a required first argument (the name of the jMaki widget to be created), and an optional second argument (a hash of the parameters to be sent to the widget). The set of parameters supported by each individual widget are the same in Ruby as for any other server side language (see the widget documentation at [1] for details), except that you express them as a Ruby hash, rather than as a JavaScript object literal. As we have seen above, this means you can intermix dynamically supplied model data from your controller, as you do with other Rails helpers.

jMaki On Rails On NetBeans

In addition to being a client side JavaScript framework, jMaki has always been supported by development tools, including plugins for both NetBeans and Eclipse. You can download these from the jMaki web site[1] as well as the framework itself. The latest version of the NetBeans plugin provides a template of available jMaki widgets, with drag and drop into your RHTML source page, just as it supports for other languages.

To use this, you will also need the latest Ruby support in NetBeans[5], which is available in the NetBeans 6.0 Milestone 9 preview.

The Future

The current code is pretty rough, and many of the widgets are not fully functional yet. But the hard part of the problems have been solved, and we're down to the details. There are lots of things that need to changed, but here are two bugs and a missing feature that are the most visible problems at the moment:

  • The current runtime for Rails does not yet understand "preload" and "postload" scripts that are supported by the Java, PHP, and JavaScript runtimes. This causes many widgets to render nothing at runtime.
  • The current runtime for Rails does not yet understand the jMaki conventions for passing API keys for things like Google Maps down to the widget.
  • For Java, JavaScript, and PHP, jMaki supports the idea of templates for preceating pages in various typical styles. The final implementation approach for this feature has not been decided yet, but my current thinking is to provide Rails generators to create layouts for the various templates. If you'd like to contribute some ideas for how this should work, please go to the jMaki web site, and join the community discussion on the user forum there.








[Trackback] Day 1 at JavaOne 07 was full of good stuff. There were a lot of announcements about FX, which sounds like an exciting new technology. I talked to a guy in the Erickson booth (You can't miss the booth, it is the only booth in the Sun section of the Pavi...

Posted by Insider Scoop From the Tutorial Divas on May 09, 2007 at 01:37 AM PDT #

This is cool!!!!!

Posted by Fred on May 14, 2007 at 03:18 AM PDT #

Post a Comment:
Comments are closed for this entry.



« July 2016