通过Flex组件间的数据组件、数据、Flex

2023-09-08 13:20:03 作者:任世事摇曳

我是新来的弯曲,所以请原谅我,如果这是一个愚蠢的问题。

现在,我使用自定义事件传递数据从一个组件到另一个。我的问题是事件只是泡沫了。我怎样才能将数据传递给一个组件,它不是组件的父调度事件?

下面是基本的布局。我试图从组件1的数据传递到组件3。

 应用程序MXML
     组件1
     组件2
          组件3
 

解决方案 Flex 3 组件实例与应用

如果一个数据需要通过在图形/树中的所有组件,最好的办法是公开每一个公用可绑定属性。让孩子组件调度是由父母,谁可以设置绑定属性的新值处理冒泡事件。如果您绑定从父属性下到孩子,这将层叠到其他组件。

 <! - 在应用程序根 - >
<的Component1的myData ={} myData的/>
 

如果您需要调用额外的逻辑,你可以定义一个get / set方法对,而不是公共变种,并添加逻辑setter方法​​:

  [可绑定]私人变种_myData;
公共功能集的myData(值:对象):无效
{
    _myData =价值;
    doSomeLogic();
}
 

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

  _myDataChanged:布尔=假;
[可绑定]私人变种_myData;
公共功能集的myData(值:对象):无效
{
    如果(_myData!=值){
        _myData =价值;
        _myDataChanged = TRUE;
    }
    invalidateProperties();
}

覆盖保护功能的commitProperties():无效{
    super.commitProperties();
    如果(_myDataChanged){
        _myDataChanged = FALSE;
        doSomeLogic()
    }
}
 

此模式用于全国各地的所有组成Flex框架的UIComponents的地方。您可能还需要重写的updateDisplayList(...)来定位元素。

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?

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}"/>

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

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

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.