Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see a couple issues before getting into the multi-module communication.</p> <p>First, in your changeView() function, you are declaring the variable action and then switching on it. </p> <pre><code>public function changeView():void { var action:String; switch(action) { // action will always be null here. } } </code></pre> <p>Because you don't have a 'default' case in your switch statement(s), parentApplication.switchView will never be called.</p> <p>Also, for the sake of brevity, you can write switch statements like this:</p> <pre><code>switch(action) { case "admin": parentApplication.changeView("admin"); break; case "tv": parentApplication.changeView("tv"); break; case "shop": parentApplication.changeView("shop"); break; // ... etc ... default: // this gets called if action doesn't match anything. break; } </code></pre> <p>Finally, you could save yourself even more typing because your action and module ids are the same, you could do this:</p> <pre><code>public function changeView(action:String):void { parentApplication.changeView(action); } </code></pre> <p>Maybe try those things and then updating your question (also, the XML for your context menus didn't render correctly in your question). That may help the community solve your issue a little easier.</p> <h2>UPDATE</h2> <p>I don't think the problem is in the module communication. I built a simple project that does what I think you're looking for. I've posted the source below.</p> <p><strong>mmodules.mxml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" implements="interfaces.IApplication"&gt; &lt;mx:Script&gt; &lt;![CDATA[ import mx.core.Container; public function changeView(action:String):void { viewstack.selectedChild = viewstack.getChildByName(action) as Container; } ]]&gt; &lt;/mx:Script&gt; &lt;mx:ViewStack id="viewstack" width="100%" height="100%"&gt; &lt;mx:ModuleLoader id="module1" url="views/module1.swf" /&gt; &lt;mx:ModuleLoader id="module2" url="views/module2.swf" /&gt; &lt;/mx:ViewStack&gt; &lt;/mx:Application&gt; </code></pre> <p><strong>interfaces/IApplication.as</strong></p> <pre><code>package interfaces { public interface IApplication { function changeView(action:String); } } </code></pre> <p><strong>views/module1.mxml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt; &lt;mx:Script&gt; &lt;![CDATA[ import interfaces.IApplication; import mx.events.MenuEvent; import mx.controls.Menu; /** * Dynamically builds a menu. */ protected function showMenu():void { var m:Menu = Menu.createMenu(null, menuData, false); m.labelField = '@label'; m.addEventListener(MenuEvent.ITEM_CLICK, onItemClick); m.show(10, 10); } /** * Handles whenever an item in the menu is clicked. */ protected function onItemClick(e:MenuEvent):void { if(e &amp;&amp; e.item &amp;&amp; e.item is XML) { changeView(e.item.@action); } } /** * Tells the parent application to switch views. */ protected function changeView(action:String):void { var app:IApplication = parentApplication as IApplication; switch(action) { case 'module1': app.changeView('module1'); break; case 'module2': app.changeView('module2'); break; } } ]]&gt; &lt;/mx:Script&gt; &lt;mx:XML format="e4x" id="menuData"&gt; &lt;root&gt; &lt;menuitem label="Module 1" action="module1" /&gt; &lt;menuitem label="Module 2" action="module2" /&gt; &lt;/root&gt; &lt;/mx:XML&gt; &lt;mx:Button label="Show menu" click="showMenu()" /&gt; &lt;/mx:Module&gt; </code></pre> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload