YOUR FEEDBACK
More on the Software Assembly Question - Do Design Patterns Help?
Yanic wrote: Hi, > UML and MDA are being changed to be more data and doc...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!

SYS-CON.TV

2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
TOP THREE LINKS YOU MUST CLICK ON


A DWR Data Store for Dojo
Putting two powerful AJAX libraries to work for you

Digg This!

Page 2 of 2   « previous page

Building a DWR Remote Proxy
This section should not be used as a tutorial on building DWR applications. It assumes that the reader has some familiarity with DWR. For further information including tutorials on this technology visit the DWR home page at www.directWebremoting.com. For our example we'll build a simple Web page with a Dojo ComboBox control that can be used to select a country. This ComboBox control will be "autocomplete"-enabled. As the user begins to type the name of a country the control will display a filtered list of country names that match the input.

First, we'll write a simple JavaBean that represents a country object as in Listing 1. The countryName attribute of this object will be used for filtering the options displayed by the ComboBox. We will also write a simple data access object named CountryDAO as in Listing 2 that can be used to get the list of all countries. (The implementation of this class is left to the reader.) This CountryDAO will be presented by DWR as a Remote Proxy that can be accessed by JavaScript on the browser. Listing 3 shows the DWR configuration required to do this.

A JavaScript on the browser should now be able to get the list of countries using the code snippet shown in Listing 4.

Building a DOJO Data Store
Now that we have a DWR remote proxy, let's build a Dojo data store that provides access to the list of countries. This section assumes some familiarity with Dojo, including classes and mixins. For more information on this toolkit read the Book of Dojo at www.dojotoolkit.org/book/dojo-book-1-0.

The Dojo.data package consists of a number of separate APIs that data stores can use. To keep our exercise simple we'll implement dojo.data.api.Read, which can read data items and their attributes.

Let's start with a shell that defines some essentials of a data store. Listing 5 shows a Dojo class named DWRReadStore. Data stores must declare the APIs they implement through a function called getFeatures. Our data store implements dojo.data.api.Read. Dojo widgets don't use data stores directly. Instead they use a mixin named simpleFetch to access data from a store. We'll mix dojo.data.util.simpleFetch into our data store. This requires that we provide a function named _fetchItems that we'll flesh out in subsequent steps.

Now that we have a basic (and essentially useless) data store we'll define some input parameters to the constructor of this class. The data store requires a reference to the JavaScript function generated by DWR - CountryDAO.getCountries. We'll pass a reference to this function to the constructor of the data store. In addition, we'll also initialize a few variables that will be used later. Listing 6 shows the changes to the constructor.

The next step is to implement the _fetchItems function. As mentioned _fetchItems is inherited from the simpleFetch mixin. This function takes three arguments, two of which are callback functions to be invoked in the case of success or failure. The first argument is a request object that encapsulates a number of input parameters from the caller. The two that should be of interest to us are "query," which is an associative array of filter parameters, and queryOptions. We'll examine how these parameters are used in the following section.

The _fetchItems function will be implemented as a two-step process. The first step will be to fetch data from the server if it has not already been loaded. This data will be cached to reduce the number of trips to the server. The second step will be to apply any necessary filters on the loaded data before returning it to the caller. Listing 7 shows the implementation of this function with simple data caching.

At this point we have a working data store. But, this data store always returns the entire data set and does not take into consideration any filter parameters supplied by the caller. We will now add the logic for handling data filtering (see Listing 8).

Finally, we will add the getValue function as in Listing 9. This function is defined in the Read API and is used by data consumers to obtain the value for any attribute of an item. In our example, the caller may use this function to get the value for the "countryName" attribute of the country item.

Using the Data Store with Dojo Widgets
At this point the data store is ready for Dojo widgets like ComboBox and FilteringSelect to use. Listing 10 shows a simple example of how this data store can be used with a Dojo widget. The use of this data store isn't significantly different from that of any other data store. It's initialized with a reference to the DWR function and applied to the widget. As the user types the name of a country into the ComboBox a dropdown list displays matching country names.

Conclusion
As demonstrated, DWR can be combined with the Dojo.data API to provide Web clients easy access to JavaBeans from a J2EE application server. DWR makes it easy to call Java classes from JavaScript, and Dojo provides a number of powerful widgets and other constructs that can be used to create a highly interactive user interface.

A number of enhancements can be made to this data store such as:

  • Allow users to provide custom implementation for getValue by passing a reference to their own function that will be called by getValue.
  • Add a mechanism to retry loading data from the server in case of a network error.
  • Refresh cached data periodically, and so on.


Page 2 of 2   « previous page

About Santosh Werghis
Sonny Werghis is an IT architect with IBM Global Services. He has over 10 years of experience with development of enterprise software using Java EE technologies. At IBM he specializes in application architecture for custom application development. His current areas of interest include Web 2.0 and RIA.

nonny wrote: Why error >> " this._dwrMetho d(function(data){ try{ self._items = data; self._loadComplete = true; self._loadInProgress = false; filter(args, self._items); }catch(e){ self._loadComplete = true; self._loadInProgress = false; errorCallback(e, args); } }); "
read & respond »
SILVERLIGHT LATEST STORIES
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
JavaOne 2008: Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
AJAX World - Xceed Launches Microsoft Silverlight 2 Control
Xceed launched Xceed Upload for Silverlight, the commercial offering in support of Microsoft's promising new Silverlight technology. The product is available now for purchase or as a fully functional 45-day trial on Xceed's website. Xceed Upload for Silverlight lets developers add uplo
Microsoft To Keynote 4th International Virtualization Conference & Expo
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
AJAX World - Curl Launches Adobe AIR Competitor
Curl announced the beta release of Curl Nitro, the code name for an extension of the Curl Rich Internet Application (RIA) platform which offers enhanced desktop capabilities required by today's enterprises. The Nitro extension simplifies the process of installing and managing Curl appl
Is the Silverlight Adoption Rate Artificially Inflated?
Silverlight 2.0 is a freaking phenomenal RIA development environment and I would actually, at this point, put the development experience in Silverlight 2.0 above and beyond Flex. I can do more faster and have it look better and run more efficiently in Silverlight 2.0 than I can in Flex
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discu
JavaOne 2008: Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-
AJAX World - Xceed Launches Microsoft Silverlight 2 Control
Xceed launched Xceed Upload for Silverlight, the commercial offering in support of Microsoft's promi
Microsoft To Keynote 4th International Virtualization Conference & Expo
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft
AJAX World - Curl Launches Adobe AIR Competitor
Curl announced the beta release of Curl Nitro, the code name for an extension of the Curl Rich Inter
Is the Silverlight Adoption Rate Artificially Inflated?
Silverlight 2.0 is a freaking phenomenal RIA development environment and I would actually, at this p
Xceed to Embrace Microsoft's Silverlight in Upcoming Product
Xceed is poised to launch Xceed Upload for Silverlight, its offering in support of Microsoft's promi
Cynergy Selected by Microsoft for Global Agency Initiative
Cynergy Systems announced it has been selected by Microsoft to participate in the Microsoft Global A
Gomez Announces Web Performance Testing Support for Microsoft Internet Explorer 8
Gomez announced support for Microsoft's Internet Explorer (IE) 8 beta 1. Using the Gomez ExperienceF
Silverlight 2 - Adobe Flex Killer Is on Its Way!
Silverlight 2.0 kicks ass and I can't wait to start dropping more hardcore blog posts regarding it.
ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES
Aplana Software Has Developed a New Web-Site for Russian TV Channel Sport Using Microsoft Silverlight Technology
Aplana Software, a Moscow based software services company and a member of I.T.Co Group,