Flex在另一个视图上执行一个功能 [英] Flex executing a function on another view

查看:162
本文介绍了Flex在另一个视图上执行一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个headerbar.mxml,当用户swipes_down在我的应用程序中显示。 headerbar.mxml包含一个按钮组件,我想在主应用程序窗口中运行一个erase()。主要应用程序是一个包含擦除()的绘图应用程序。我只是不知道如何从另一个mxml视图文件调用一个函数。我认为这将是点击={mainwindow.drawPanel.erase()};
$ b

编辑:

  protected function onColorChange(event:List):void {
appHome.drawArea.erase();


解决方案

视图(AKA组件)它在很大程度上取决于体系结构。这听起来像你想在你的父母运行一个功能。在这种情况下,'封装正确'方法是从component1派发一个事件;在component1s parent中监听事件;并从事件监听器中执行该函数。

因此,在headerbar的父节点的某处添加事件监听器:

  headerbarInstance.addEventListener('parentDoSomething',onHeaderBarToldMeTo); 

我可能会在构造函数中加入ActionSCript 3组件或preinitialize事件处理函数如果一个MXML组件。 'parent'组件还需要监听器函数:

pre $ protected $ function onHeaderBarToldMeTo(event:Event):void {$ b $删除();



$ b当点击headerbar.mxml中的按钮组件时,事件处理程序,它需要调度事件,如下所示:

  protected function onButtonInheaderbarClick(Event:Event):void {
dispatchEvent(new Event('parentDoSomething'));
}

一切都应该神奇地工作。如果函数不在父类的直接子元素中,则可能需要冒泡。



如果您不关心封装,也可以访问父母直接。所以你的标题栏组件会做到这一点:

  parent.erase(); 

这很简单直接,应该可以工作,但从维护的角度来看,这被认为是非常糟糕的做法。

I have a headerbar.mxml that is displayed when user swipes_down in my app. The headerbar.mxml contains a button component I want to run an erase() in the main application window. The main application is a drawing app that contains an erase(). I just don't know how to call a function from another mxml view file. I thought it would be something like click="{mainwindow.drawPanel.erase()}";

EDIT:

protected function onColorChange(event:List):void{
                appHome.drawArea.erase();
            }

解决方案

To run a function in another view (AKA Component) It depends largely on the architecture. It sounds like you want to run a function in your parent. In which case the 'encapsulation proper' method is to dispatch an event from component1; listen for the event in component1s parent; and execute the function from the event listener.

So, somewhere in headerbar's parent, add the event listener:

headerbarInstance.addEventListener('parentDoSomething', onHeaderBarToldMeTo);

I'd probably add this in the constructor if an ActionSCript 3 component, or in a preinitialize event handler if an MXML Component. The 'parent' component will also need the listener function:

protected function onHeaderBarToldMeTo(event:Event):void{
  erase();
}

When the clicks the button component in headerbar.mxml and this fires off a click event handler inside of headerbar, which needs to dispatch the event, like this:

protected function onButtonInheaderbarClick(Event:Event):void{
 dispatchEvent(new Event('parentDoSomething'));
}

And everything should magically work. You may have to bubble the event if the function is not inside a direct child of the parent.

If you don't care about encapsulation, you can also just access the parent directly. So your header bar component would do this:

parent.erase();

It's simple and straight forward, and should work, but is considered horribly bad practice from a maintenance stand point.

这篇关于Flex在另一个视图上执行一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆