开发者

flex: call parent method from module using Interface

I am using this guide for passing data to modules "Using interfaces for module communication". For getting child module instances they have done this

var ichild:* = mod.child as IModuleInterface; (mod = moduleLoader)

What should I do to get instance of parent application insi开发者_高级运维de module? How can I call parent methods inside modules?


It's easy. Just pass any class instance in your main application to module, which methods you want to call.

Your module:

<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script><![CDATA[

        public var appInst : Object;

        public function CallAlert() : void
        {
            if (appInst != null)
                appInst.AppAlert("Hello from module");
        }

        ]]></mx:Script>
    <mx:Button click="CallAlert()" label="click"/>
</mx:Module>

Your main application:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script><![CDATA[
        import mx.controls.Alert;

        public function AppAlert(str : String) : void
        {
            Alert.show(str);
        }

        public function ready(evt : Event) : void
        {
            mod.child["appInst"] = this;
        }

        ]]></mx:Script>
    <mx:ModuleLoader
            id="mod"
            width="100%"
            url="module.swf"
            ready="ready(event)"/>
</mx:Application>

The [ ] operator is an alternative way to use properties and methods of objects. We can't use here mod.child.appInst here, because mod.child is DisplayObject, and it has no such property. But our module main class has property appInst. It's an alternative way to using interface. You may pass any variables or functions to your module application. So that's it.

P.S. Be careful with errors of type casting and unexisting properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜