views:

4326

answers:

3

hi,

how can i pass values from my custom components back to the main.mxml? i need to do this to pass back ana array collection

A: 

got it; just needed to import mx.core.Application;

combi001
+4  A: 

You can call directly into Application using the static Application.application.yourPublicMethodName() or .yourPublicPropertyName = n, but you might also consider using the event framework, to keep your components loosely coupled. Since your component is by definition an event dispatcher, you can simply dispatch an event from within it, and have your Application class listen for that event.

In MXML, that looks something like this, for the component class:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">

    <mx:Metadata>
     [Event(name="buttonClicked", type="flash.events.Event")]
    </mx:Metadata>

    <mx:Script>
     <![CDATA[

      public var someValue:int = 0;

      private function buttonClick():void
      {
       someValue = 1;
       dispatchEvent(new Event("buttonClicked"));
      }

     ]]>
    </mx:Script>

    <mx:Button label="Click Me" click="buttonClick()" />

</mx:Canvas>

... and for the Application, like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

    <mx:Script>
     <![CDATA[

      import mx.controls.Alert;

      private function myComponent_buttonClicked(event:Event):void
      {
       Alert.show(event.currentTarget.someValue.toString());
      }

     ]]>
    </mx:Script>

    <local:MyComponent buttonClicked="myComponent_buttonClicked(event)" />

</mx:Application>

The Event metadata tag in the component class tells the compiler the component dispatches a flash.events.Event-type event ("buttonClicked"), which exposes it as an event on the MyComponent tag; then, all you need to do is wire up a listener for that event, and through the event's currentTarget property, you get access to all of the component's public data.

Just figured I'd offer up an interesting alternative for ya. Hope it helps!

Christian Nunciato
A: 

How I can implement this <local:MyComponent buttonClicked="myComponent_buttonClicked(event)" /> in action script

danny
http://stackoverflow.com/questions/ask
Amarghosh