YOUR FEEDBACK
Gregor Rosenauer wrote: well, not what's your take on this? Did I miss a second page of this article or...
AJAXWorld RIA Conference
Early Bird Savings Expire Friday Register Today and SAVE !..


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
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
SYS-CON.TV
TOP COLDFUSION LINKS


Completing The Real Estate Sample Application
Managing property listings through Flash Forms PART 2

If you recall from Part 1, you wrote a function within your form called setUpRemoting() that created the Flash Remoting service connection. When you created this connection, you had to supply an object to be called when the server returns a response, as follows:

RealEstateAdmin.myGlobalObjects.listingService =
connection.getService("DevnetRealState.services.ListingService", responseHandler );

The object called when the server returns a response is the responseHandler object. When you created it, you also implemented two functions: the onResult and onStatus functions. If you don't modify this object and use this instance of the handler object to call the update function, the onResult function of responseHandler object will be called when the server sends the response. This is because the onResult function is the default function that Flash Remoting uses if you specify nothing else.

The problem is that you used the onResult function to set the datagrid's dataProvider with the returned results. That is not preferable for all the other service calls. To handle those calls, you must implement other functions in the responseHandler object. When you don't wish to use the two default functions (the onResult and onStatus functions), you must create one function for each service method you would like to call.

The function name should be the name of the service method followed by _Result. For example, if you want to call the update method, you will need to add a function called update_Result() to the handler object (responseHandler object in this example):

    // handle create response
responseHandler.create_Result = function( results: Object ):Void {

}

    // handle update response
responseHandler.update_Result = function( results: Object ):Void {
}

    // handle remove response
responseHandler.remove_Result = function( results: Object ):Void {
}

Next you'll see how to implement these functions.

Adding a New Record
The create() method of the service CFC (realestate/services/ListingService.cfc) inserts a new property listing into the database by calling the create() method in the ListingDAO.cfc (realestate/components/ListingDAO.cfc). Then the CFC returns a structure with status, message, and item keys.

If the CFC was able to insert the item successfully, it returns the status field as true and an item field containing the complete newly inserted record. This is useful not only if the data in the form fields changes while the service responds but also to get any field that may have changed or may have been calculated on the server.

What do you think the create_Result() function does with the data the service returns? You probably guessed that you need to add this new record to the datagrid so that it's available for editing. For simplicity's sake, add it to the top of the grid (only if the returned status is true) using the addItemAt() function of the datagrid. This function takes the index where to insert the item and the actual item to insert. Because the CFC returned the complete item, you can supply it directly to the datagrid by getting it from the results object and passing it as the second addItemAt() parameter:

listingGrid.addItemAt(0,results.item);

This following is the complete create_Result() function:

responseHandler.create_Result = function( results: Object ):Void {
      //only add the item to the datagrid if creation was successful
    if (results.status){
      //add an item at the top of the grid, containing
      //the item that was returned in the "item" field
      //of the results
      listingGrid.addItemAt(0,results.item);
      alert("Item inserted");
    }
    else {
      alert("Item could not be inserted");
    }
      //remove the clock cursor
      mx.managers.CursorManager.removeBusyCursor();

}

Updating a Record
The update_Result() function that you need to implement in the responseHandler object to handle the results of a call to the update() service method is similar to the create_Result() method. In this case, however, edit the field that has been updated instead of adding a new item to the datagrid.

The problem with asynchronous calls is that you don't know when the results will return and whether the form will be in the same state as when the user made the original call. You cannot assume that the updated record is still the grid's selected item. Just as you created a helper function to select items in a select control (with the cfselect tag), you can create a helper function that finds an item by the ID (mls_id in this case) in the datagrid, and returns its index. Then you will be able to edit the correct item.

Write this function inside a <cfformitem type="script"> block as follows:

public function findItem
(grid:mx.controls.DataGrid, columnToSearch:String, id:String):Number {

    for (var i = 0; i < grid.dataProvider.length; i++){
      if (grid.getItemAt(i)[columnToSearch] == id){
      //found
        return i;
}
    }
    //not found
    return -1;
}

When the service returns the results, you look for the item updated in the grid and change it using replaceItemAt() function of the datagrid. This function takes the index of the record to edit and the new value to assign to the row. Again, you can supply the returned item as the new value to the replaceItemAt() function (see Listing 4).

Deleting a Record
When a user selects an item in the datagrid, he or she can also remove it from the database by clicking on the Remove Property button. Using the same technique as you used for the other buttons, call a function when the user clicks the Remove Property. You will name this function submitRemove():

<cfinput type="button" name="removeBtn" value="Remove Property"
onclick="submitRemove()" />

The service connection calls the remove function on the service CFC to delete items. That function only requires the mls_id of the item to remove, so you need only get that field from the form and send it. The field is an input called edit_mls_id, so you'll access its current data through edit_mls_id.text:

public function submitRemove():Void {
      <!--- call service, sending the id --->
    RealEstateAdmin.myGlobalObjects.listingService.remove(edit_mls_id.text);
}

As with the other functions, put it into a <cfformitem type="script"> block.

When the service sends a successful response, the responseHandler object that receives the response must remove the item from the data grid, because it no longer exists in the database. Because you don't know which row holds this item, you can use the same helper function, findItem(), to look for the item to delete.

About Nahuel Foronda
Nahuel Foronda is one of the founders of Blue Instant (http://www.blueinstant.com), a web development firm specializing in Rich Internet Applications where he has been creating award-winning applications and offering training for the last five years. He also maintains a blog, called AS Fusion (http://www.asfusion.com), where he writes about Flash, ColdFusion and other web technologies.

About Laura Arguello
Laura Arguello is one of the founders of Blue Instant (http://www.blueinstant.com), a web development firm specializing in Rich Internet Applications where she has been creating award-winning applications and offering training for the last five years. She also maintains a blog, called AS Fusion (http://www.asfusion.com), where she writes about Flash, ColdFusion and other web technologies.

CFDJ LATEST STORIES . . .
Rich Internet Applications offer the potential to fundamentally change the user experience and in doing so, yield significant business benefits. The theme of this October's AJAX World Conference & Expo 2008 West is 'Beyond AJAX to the RIA Era' and the Call for Papers, which is still op...
Join Scott Guthrie as he discusses Microsoft’s commitment to web standards development, Rich Internet Applications and how Microsoft is contributing to help move the web forward. Join Adobe’s Kevin Lynch as he demonstrates how Flash and HTML come together to make the most engaging,...
Virtualization has become a critical part of Enterprise IT strategy. Why and how has it become one of the most important change agents in our industry? To answer these questions I had the good fortune recently to be able to speak to a select group of top IT industry executives who join...
SQL Injection attacks are one of the easiest ways to hack into a website. One recent hack, using a script from verynx.cn, involves injecting sql into a web form that then appends some JavaScript code into fields in a database that then gets executed on the client side when a user views...
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p...
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T...
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

MOST READ THIS WEEK
ADS BY GOOGLE