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

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

问题描述

我有一个 headerbar.mxml,当用户在我的应用程序中 swipes_down 时显示.headerbar.mxml 包含一个按钮组件,我想在主应用程序窗口中运行 erase().主应用程序是一个绘图应用程序,其中包含一个erase().我只是不知道如何从另一个 mxml 视图文件中调用函数.我认为它会类似于 click="{mainwindow.drawPanel.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()}";

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

推荐答案

在另一个视图中运行一个函数(AKA 组件)很大程度上取决于架构.听起来你想在你的父母中运行一个函数.在这种情况下,适当封装"方法是从 component1 调度事件;在组件 1 的父级中侦听事件;并从事件监听器执行函数.

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);

如果是 ActionSCript 3 组件,我可能会在构造函数中添加它,如果是 MXML 组件,我可能会在预初始化事件处理程序中添加它.父"组件也需要监听函数:

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

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

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天全站免登陆