May 29 2007

Developing a Real-World Web Application with NetBeans 5.5 Visual Web Pack

David Botterill walk the audience through the design, analysis, issues, and resolutions of creating the NetBeans plugin portal site using NetBeans, of course, and JavaServer Faces (JSF). Some of David’s best practices for JSF development include are to come up with a preliminary set of requirements, use best guess at page design and page flow in prototype to get feedback, solidify the UI during analysis (the UI will effect the architecture), use Firefox Firebug to debug tricky HTML, CSS, and JavaScript.

Many of the issues described in by David seemed to have been web pack bugs that have since been fixed. That said, he did suggest some common pitfalls and how to avoid them. David suggested to start with Plain Old Java Objects, POJOs, not with relational tables and foreign keys, and joins, etc. David also stated that getting started with JPQL queries was a bit difficult, if for no other reason than it is something new that he had to go through. David found it difficult to have a button for a form which you can just press the enter button to submit it because there was no submit button on the form. Be aware of session timeouts, don’t make assumption in your software about the state of the session, always test for it. Be diligent about separating your CSS in separate files from your HTML. If you have a web application that relies heavily on the GET method then reevaluate using JSF because it makes it difficult.

Finally, David believes that for web applications resiliency is better than efficiency.

Technorati Tags: , , , , , ,


May 28 2007

Using jMaki in a Visual Development Environment

The jMaki project started as a wrapping utility for JS libraries and widgets. The j stands for JavaScript and maki comes from the Japanese word for wrapping, maku. The intent of jMaki is to promote clean seperation between content, style, and javascript and do so in an abstraction layer that lends itself well to a component model. jMaki is a client-server framework with support in PHP, Rails, or Java web applications.

jMaki has nice IDE support in NetBeans with plenty of wizards to get you started. Since jMaki provides a nice component model you can use the visual editor to wire together the UI of a web application in your IDE. Since a big part of web application development is getting the layout right, jMaki comes with several layouts right out of the box, all you do is select the template you want to use from the IDE.

To wrap a custom JavaScript library with jMaki you will need to create a jMaki widget. A jMaki widget uses the convention, instead of configuration, which is basically a folder containing three files, a HTML template, a CSS style, and JavaScript behavior. jMaki already has support for popular JavaScript libraries such as ExtJS, YUI!, Dojo, etc.

Greg Murray stated that he developed jMaki because he “wanted the reload button to be the redeploy button.” Well, since HTML, CSS, and JS aren’t compiled, that is easy to do. One button redeploy is not the stregth of jMaki, what jMaki has going for itself is that you can visual develop a web application using NetBeans and that this wraps several libraries in a easy to use fashion. jMaki also allows you to mix and match different JavaScript libraries together and communicate with each other using Glue. jMaki provides a common ground for integrating multiple of the currently existing AJAX libraries.

There is a jMaki on Rails plugin available and a PHP library.

Technorati Tags: , , , , , , , , , , ,


May 18 2007

CommunityOne 2007: Ajax Applications Made Easy with jMaki and Scripting

This technical session at CommunityOne 2007 introduced me jMaki, a project under the GlassFish umbrella. Project jMaki is a lightweight AJAX framework that wraps JavaScript libraries and in such provides a common interface to any and all JavaScript code. jMaki proposes a convention, over configuration, to wrap any third-party or custom JavaScript library as a jMaki widget. In other words, jMaki is a widget model for creating, using, and wrapping existing JavaScript functionality. jMaki is also available as a helper in Rails, and as a function in PHP, as well as a component in JSF.

Out of the box, jMaki comes with layout and theme support. In addition to layout, jMaki has a XMLHttpProxy (XHP) feature that allows you to bypass the pesky same-origin policy restriction.

Should you use jMaki? Yes. jMaki does all the hard work so that you can mix YUI!, Scriptaculous, Spry, and any other Ajax library together seamlessly. jMaki provides visual development support with plugins for Eclipse and NetBeans. jMaki also provides a common widget model for JavaScript libraries so that you don’t have to learn each more than you have to to work with any one of them.

Technorati Tags: , , , , , , , , , , ,


May 2 2007

Unobtrusive JavaScript with Prototype and Behavior

If you are familiar with web application development you may already be familiar with the Model-View-Controller (MVC) design pattern. The MVC pattern aims to separate your server side business logic from your presentation code from your data model. The by-product of web applications are the HTML, CSS, and JavaScript pages generated and delivered to the client browser. Historically in the early Web 1.0 days, a web page’s content, style, and behavior would all be intertwined and glued tightly together in the HTML, do you remember the font tag? Somewhere along the way to the Web 2.0 network, developers opted to remove the style attributes from the content by using Cascading Style Sheets (CSS) classes.

CSS allows you to create style classes whose concern is purely a way to describe the size, font, color, and look of whatever content that uses the class.

.makeMeBeautiful {
  color: #00248F;
  text-decoration: underline;
  background-color: #FFFFFF;
  font-size: 1.1em;
}

To apply a CSS class on an HTML element you use the class attribute.

<div class="makeMeBeautiful">Hello, World</div>

The idea behind Unobtrusive JavaScript (UJS) is to remove JavaScript events, such as onclick and onmouseover, from the content in the same fashion as the style attribute. Unobtrusive JavaScript uses the class and id attribute of HTML tag elements to find and assign JavaScript events in a inconspicuous fashion, just as with CSS.

If you think of the HTML content as the Model, the CSS stylings as the View, and JavaScript behaviors as the Controller of MVC, then it makes perfect sense to remove all traces of style attributes and JavaScript events from the HTML.

Unobtrusive JavaScript with Prototype

Traditionally if you wanted to add a JavaScript onclick event to a HTML element you would do so by adding the onclick attribute. Unobtrusive JavaScript recommends that you instead find the tag by the class or id attribute and assign the even programmatically.

// Logic to execute when the end user
// clicks the element
function myOnClickFunction(elem) {
  alert(elem);
}

// Register the onclick event for all elements
// the given class name
window.onload = function() {
  // Find all elements that use that given CSS class
  var elements = document.getElementsByClassName("makeMeBeautiful");
  elements.each(
    function(e) {
      // Assign the onclick method to the element
      Event.observe(e, "click", myOnClickFunction);
    }
  );
}

Note that the above JavaScript code works only when the Prototype JavaScript library is made available.

If your need to add Unobstrusive JavaScript events for a few tag elements and are already using Prototype then the above code will suffice.

Unobtrusive JavaScript with Behavior

For more restrictive selection process of the HTML elements to apply JavaScript events you can use the Behavior JavaScript library. The Behavior JavaScript library is a lot easier and more powerful way to add behavior to HTML elements than Prototype. With the Behavior library you define an associated hash map literal object thingie where the keys are CSS selectors and the value is an initializing JavaScript function. In the initializing function you can add any onclick, onmouseover, onchange, onsubmit, onfocus, onblur, onwhatever event.

var rules = {
  '.makeMeBeautiful' : function(elem){
    elem.onclick = function(){
      alert(elem);
    }
  }
};

Behaviour.register(rules);

The power of the Behavior library is that you can create the behavioral rules using CSS selectors to choose the tag elements for which to apply a given set of JavaScript functionality. Both the Prototype and Behavior code shown up to this point will apply the JavaScript onclick behavior on all elements that use the given CSS class. With the Behavior library you can use CSS selectors to restrict what elements to apply the behavior to. For example, to apply the onclick behavior only for div elements you can use the following rule.

var rules = {
  'div.makeMeBeautiful' : function(elem){
    // Your code here...
  }
};

The idea behind Unobtrusive JavaScript is to separate the HTML, CSS, and JavaScript according to responsibilities in a similar fashion as the MVC design pattern suggests. UJS also helps in the maintenance of large sites and it allows for graceful degradation.


Apr 19 2007

Google AJAX Feed API

Google recently made available a new JavaScript-based API for downloading RSS and Atom feeds. Up to now I have been using Magpie RSS for downloading, caching, and displaying feeds. I was just thinking of tinkering with Feedtools for manipulating and handling feeds in Ruby/Rails, but I would much rather like for Google to deal with, cache, and pay for the bandwidth of all the RSS feeds that I consume.

You will need to get a developer key, just like for all other of Google’s APIs, before you start using the Google AJAX Feed API. Once you have a developer key, they will give you all the JavaScript code you need to start displaying feed entries, well you might want to configure the feed format and/or how many entries you want.

As an example of what you can do, or of what others have done which you can use, with the new Google AJAX Feed API check out Feed Billboard.

Technorati Tags: , , , , , , , , , ,


Feb 7 2007

Rails Google Maps Plugin

I still remember the awe that struck me when I first saw for the first time Google Maps in action. I was able zoom in and out, and even circumnavigate the globe by just clicking and moving the cursor around. Google Maps really demonstrated what a web 2.0 mashup could be like. Housing Maps is mashup between Google Maps and Craigslist apartment/housing ads. If you want show all the locations of Wi-Fi hot spots in your Rails app like Hotspotr, then you need to take a look at Yellow Maps 4 Ruby on Rails Plugin for Google Maps.

Before you get started you will need to sign up for a Google Maps API access. Once you have your Maps API key install the Rails plugin by executing the ‘script/plugin install <URL>’ command from your application directory where URL is replaced with the following.

svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm

The installation process will copy files in the under config, vendor/plugins, and in the public/javascripts directory. Open config/gmaps_api_key.yml in your favorite text editor and add your Google Maps API key under production for you domain. It was my experience that I did not have to change the Google Maps API key for the development and test environment. Now you are ready to start producing maps.

Update the HTML header of your template layout file so that some required JavaScript include statement are insert. Next to other javascript_include_tag statement add the following.

<%= GMap.header %>

In the controller you instantiate a GMap object with a id name. You will also need to supply the center of the map with the longitude and latitude and the zoom factor. If you want a little map balloon to appear you will need to create a marker. Your map can have any number of markers, each position separately with a title and info window when clicked. Here is the code used to construct a map, which can be placed in your controller.

@map = GMap.new("map_div_id")
@map.control_init(:large_map => true, :map_type => true)
@map.center_zoom_init([75.5,-42.56], 4)

marker = GMarker.new([75.6, -42.467],
  :title => "Where Am I?", :info_window => "Hello, Greenland")
@map.overlay_init(marker)

To display your map in the action’s rhtml view file you will need to lines of code. The first line will produce JavaScript code that describes your map. The second line of code will produce the div that will display the map. If any of these two lines are omitted the map will not be displayed.

<%= @map.to_html %>
<%= @map.div(:width => 600, :height => 400) %>

These are the basics of using Google Maps in your Ruby on Rails application but YM4R can do a whole lot more. Do take the time to read the Google Maps FAQ and the YM4R GM ruby docs.