Search code examples
apache-flexactionscript-3moduleflex3flexbuilder

Flex: use function of parentApplication from a module, but with some kind of API


I have an application which loads modules. These modules could be developed by third parties. So to make it easier for them, I would like to develop an API so they know which functions they can call and use.

So I know this can work within a module, but then you have to know there is a var "expenses" in the parentApplication:

// Access properties of the parent application.
private function getDataFromParent():void {
    expenses = parentApplication.expenses;
} 

But what I'd prefer is something like this:

private function getDataFromParent():void {
    var myParent : MyAPI = new MyAPI();
    expenses = myParent.getExpenses();
} 

So if it would work like this, when you start coding and type "myParent." Flex would suggest all the possible functions and variables with code hinting.

Any ideas how to get started with this?

Thanks a lot,

Frank


Solution

  • I'd make an interface for the "parent" and one for the "modules". This let's you handle all the modules the same way, and makes it easy for the individual module developers to know what the "parent" provides.

    public class IModule {
        // when initialize() is called it will be up to the module's developer to 
        // retain a reference to the parent and use that for later calls
        function initialize(parent:IParent):void;
        function doStuff():Boolean;
    }
    
    public class IParent {
        function getInfo():String;
        function foo(value:int):int;
    }