|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $50 Savings Expire June 24, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP LINKS YOU MUST CLICK ON Data
A DWR Data Store for Dojo
Putting two powerful AJAX libraries to work for you
By: Santosh Werghis
Apr. 14, 2008 02:00 PM
Digg This!
Page 2 of 2
« previous page
Building a DWR Remote Proxy 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 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
Conclusion A number of enhancements can be made to this data store such as:
Page 2 of 2 « previous page
ENTERPRISE OPEN SOURCE MAGAZINE LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||