在 flex 组件之间传递数据 [英] Pass data between flex components

查看:24
本文介绍了在 flex 组件之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 flex 新手,所以如果这是一个愚蠢的问题,请原谅我.

I'm new to flex , so forgive me if this is a dumb question.

现在我正在使用自定义事件将数据从一个组件传递到另一个组件.我的问题是事件只会冒泡.如何将数据传递给不是调度事件的组件的父组件的组件?

Right now I'm using custom events to pass data from one component to another. My problem is that events only bubble up. How can I pass data to a component that isn't a parent of the component dispatching the event?

这是基本布局.我正在尝试从组件 1 中获取传递给组件 3 的数据.

Here's the basic layout. I'm trying to get data from component 1 passed to component 3.

Application MXML
     Component 1
     Component 2
          Component 3

推荐答案

如果图/树中的所有组件都需要一条数据,最好的办法是在每个组件上公开一个公共的可绑定属性.让子组件调度一个由父组件处理的冒泡事件,父组件可以设置可绑定属性的新值.如果您将属性从父级绑定到子级,这将级联"到其他组件.

If a piece of data is required by all components in a graph/tree, your best bet is to expose a public bindable property on each. Let the child components dispatch a bubbling event that is handled by the parent, who can set the new value of the bindable property. If you bind the property from the parent down to the child this will "cascade" down to the other components.

<!-- in root application -->
<Component1 myData="{myData}"/>

如果你需要调用额外的逻辑,你可以定义一个 get/set 对而不是 public var 并向 setter 添加逻辑:

If you need to invoke additional logic, you can define a get/set pair instead of public var and add logic to the setter:

[Bindable] private var _myData;
public function set myData(value:Object):void
{
    _myData = value;
    doSomeLogic();
}

更好的是使用 Flex 的失效框架来优化性能:

Even better would be to use Flex's invalidation framework to optimize performance:

_myDataChanged : Boolean = false;
[Bindable] private var _myData;
public function set myData(value:Object):void
{
    if (_myData != value) {
        _myData = value;
        _myDataChanged = true;
    }
    invalidateProperties();
}

override protected function commitProperties() : void {
    super.commitProperties();
    if (_myDataChanged) {
        _myDataChanged = false;
        doSomeLogic()
    }
}

这种模式在构成 Flex 框架的所有 UIComponent 中无处不在.您可能还需要覆盖 updateDisplayList(...) 来定位元素.

This pattern is used all over the place in all the UIComponents that make up the Flex framework. You may also need to override updateDisplayList(...) to position elements.

这篇关于在 flex 组件之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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