Universal Application To Module Api

While working on my latest project, I was supposed to came up with some elegant solution (api) to make 3rd party modules be able to communicate with the main application and vice versa. In fact I already did the approach some time ago, now it was time to improve it and implement into a flex framework.

The api is based on event dispatching. Instead of sharing interface and calling some public available functions / callbacks (requires functions defined in compile time), event based api gives you the freedom of registering and releasing listeners anytime in runtime…

I have chosen the child Module (instance) as the api calls hotspot. On this place both parent and child objects dispatch and listen for api calls. The AbstractApi class extends Event and implements IEventDispatcher. This construction lets you dispatch events over api call itself what is crucial when handling asynchronous response. See the demo and source for more info.

Download sources from here.

To better understand asynchronous result handling, see the following code. First, the listener for Event.COMPLETE is registered on apiCall, than the apiCall is dispatched. Event.COMPLETE is dispatched once apiCall.complete() method called.

private function onClick():void
    var apiCall:ExpectDataApi = new ExpectDataApi();
    apiCall.addEventListener(Event.COMPLETE, onExpectDataApiComplete);

// Event.COMPLETE dispatched with apiCall.complete()
private function onExpectDataApiComplete(event:Event):void
    var apiCall:ExpectDataApi = ExpectDataApi(event.target);

Tip: I use prefix Expect* for api calls expected by main application and Dispatch* for api calls dispatched by main application. It helps not to mess up api suported by main application and api supported by module.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.