|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF & Flash Completing The Real Estate Sample Application
Managing property listings through Flash Forms PART 2
Mar. 14, 2006 05:00 PM
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 = 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 Next you'll see how to implement these functions.
Adding a New Record 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 {
Updating a Record 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 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
<cfinput type="button" name="removeBtn" value="Remove Property" 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 { 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. CFDJ 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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||