Ok so around the days when I first started working with Flash Remoting, I was working with the FTree Component and filling it from a hierarchical structure on the back-end. It was basically an admin tool for editing the structure (adding nodes, deleting them, renaming them) I quickly ran into something that frightened me though, asynchronous calls. At the time I was always using the defaultResponder parameter when "getting" the service. So I had no way to take a delete action, know if it was successful and if so delete that node in the IDE. Several calls to the back-end are not guaranteed to fire the result handler in the same order. I could have modified the cfc to pass back the ID of the node that was deleted, but then I would have to walk my flash tree to find that id since the id's did not sync up. So I wrote this whole grand schema extending upon remoting to distinguish calls...waste of time. Here is the most elegant, simple way to handle my problem. (and possibly yours)
/* typical stuff */
NetServices.setDefaultGatewayURL("somegatewayurl.com");
gatewayConnection = Netservices.createGatewayConnection();
/* Look! No second parameter */
MyService = gatewayConnection.getService("some.service.here");
DeleteNodeHandler = function(node,tree){
this.node = node;
this.tree_ref = tree;
}
DeleteNodeHandler.prototype.onResult = function(res){
if(res==false) return;
/* Delete was a success, so remove the node this instance refers to */
this.tree_ref.removeNode(this.node);
}
function deleteNode(node){
/* since we never set a defaultResponder we can pass one in on each method call */
MyService.deleteNode(new DeleteNodeHandler(node,myTree), node.getData().id);
}
So the process that happens here is this:
1. The user requests to delete a node.
2. The deleteNode function calls the server side deleteNode method and passes in a new DeleteNodeHandler, which takes the node id and a reference to the tree that its being deleted from. That new object stores that for later use. Then it passes the rest of the parameters that the server side will use (the id referring to the id on the back-end)
3. If the method call returns anything but false (meaning it was successful at deleting the node from the server side structure) it continues on to delete it from our UI construct.
So now we can call deleteNode one thousand times in a row and its guaranteed to stay in sync with the back-end.
12:43:11 PM
|
|