开发者

dispatch function event from application to component

开发者 https://www.devze.com 2023-03-03 18:28 出处:网络
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,

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 ru开发者_StackOverflow社区n():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?


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>


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();
}
0

精彩评论

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