Flex: Callback of a Callback
Depending on architecture of your Flex application (or application you work on), you might have to use callback functions, or it is mandatory to use them. Of course, you shouldn’t be mixing things inside so service functions should remain in service class, where you call your remote objects. Callback function populates your model and that is it. We shall not get into merit should you use MVC, callback functions etc. but presume that you have to do it this way.
So, this is a typical remote service call:
public function getMyArrayCollection(param:SomeObject):void { var remote : RemoteObject = new RemoteObject('WebLibrary.Services.MyService', getMyArrayCollectionCallback); remote.getOperation("getMyArrayCollection").send(param); } private function getMyArrayCollectionCallback(evt:ResultEvent):void { model.myArrayCollection = evt.result as ArrayCollection; }
OK, now when you call getMyArrayCollection, it will go to server and bring back collecion of result objects and update model. Problem is that, for now, you don’t have a clue when your call have completed operation, and if your view needs to know when operation is completed, you have a problem.
So, let’s say your view (screen from where you called a service) need to know when operation is completed and perform some other tasks. There are a lot of ways to do it, like using event broadcasting, monitoring change of your arrayCollection inside model etc.
I have seen a code that implements callback inside the view so when service ends, it returns to callback procedure inside of a view. That is wrong programming practice as you may to write more callbacks inside other views that calls same remote procedure, and then, you will have a soup of scattered callback functions which will be extremely difficult to maintain.
However, there is a simple way to overcome this problem. I like to call it “callback of a callback”. In this case, remote calling function will have a function as a parameter which will be called when it finishes operation completely. This is done by creating one variable of type OBJECT inside your service class.
Let’s re-mount the same functions from above.
private var callbackObject:Object = new Object(); public function getMyArrayCollection(param:SomeObject, callbackFunction:Function=null):void { var remote : RemoteObject = new RemoteObject('WebLibrary.Services.MyService', getMyArrayCollectionCallback); remote.getOperation("getMyArrayCollection").send(param); if (callbackFunction != null) { callbackObject.getMyArrayCollection = callbackFunction; } } private function getMyArrayCollectionCallback(evt:ResultEvent):void { model.myArrayCollection = evt.result as ArrayCollection; if (callbackObject.getMyArrayCollection != null) { callbackObject.getMyArrayCollection(evt.result as ArrayCollection); } }
So now, you can write your own callback function wherever you need it, and pass it as a parameter to a calling function. It’s original callback will update model, and then, if custom callback function is made, it will call it when done. This is how you don’t mix things. Service callback does what it originally have to do (update model), and it executes your code wherever it is.
If you enjoyed this post, make sure you subscribe to my RSS feed!


