0

I will need to execute a function in a comp1.mxml from main.mxml through event, I found it difficulty to understand and unable to get it work. Suppose,

main.mxml

public function run():void {
//call a function in comp1.mxml
}

and in a comp1.mxml:

public function runComponent():void {

}

Is metadata is need in this case and how to make it work?

2 Answers 2

1

It really depends what you're trying to do, but how it works is that the main app just calls the public function on it's children and not using event.

The other way would be to use an application framework like Parsley, RobotLegs or Swiz so that you can do those kinds of 'connections', but that might not be desirable in this case.

So yeah, I think what you want to do is something like this:

<s:Application creationComplete="comp.runComponent()">
   <comp:Comp1 id="comp" />
</s:Application>
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! thank you, to think it took me 6 hours just to get a simple answer, didn't know about this. How did you know it?
It's fairly basic. This is OOP with a view (which is Flash essentially) but done in an xml way (Flex).
Done a lot XML stuff in Flex too too especially MusicXML
0

The main goal of event model is to implement Observer pattern to provide low coupling between components. Lets we have a component called main.mxml which contains comp1.mxml. So main.mxml knows about comp1.mxml and it is normal. main.mxml can call public methods of comp1.mxml without problem.

The event model gives us a possibility for comp1.mxml to not know about main.mxml. main.mxml subscribes to comp1.mxml events and comp1.mxml fires them calling methods of main.mxml without coupling.

According to your question you want to do something opposite. I think it is not a proper way. Do not use events to call methods of comp1.mxml from main.mxml. Just call runComponent() directly the following way:

public function run():void {
    myComp1Instance.runComponent();
}

4 Comments

Yes, I missed this part. But have you had any experience with as3signal?
I have no practical experience with it. What about me personally I see no reason to have another event model as far as AS event model is native. Anyway as3signals have the same base conception (implementation of Observer pattern). So my answer is valuable in case of using as3signals.
I will give a try for DAW workstation, I personally still skeptical of using MVC even when RobotLegs are more popular. If by separate components into different .mxml/.as can do the same?
Observer (as many other basic design patterns) is applicable to AS-MXML components without any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.