Transferring data between flex components

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

I am now using custom events to transfer data from one component to another. My problem is that events are only bubbling. How do I pass data to a component that is not a parent of the component that sends the event?

Here is the basic layout. I am trying to get data from component 1 passed to component 3.

Application MXML
     Component 1
     Component 2
          Component 3
+3
source share
3 answers

/, . , , bindable. , "" .

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

, get/set public var setter:

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

Flex :

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

UIComponents, Flex. updateDisplayList (...) .

+10

- .

<Component1 name="component1/>

<Component2 name="component2" onClick="onClickHandler()"/>

private function onClickHandler(event:MouseEvent):void
{
    component1.property1 = "Random data";
    component1.sendData("Random Data");
}

bindable 1, PropertyChangedEvent, .

, , , .

: , , 3 2, 3 1? 2 (, , ).

private function component1OnClickHandler(event:MouseEvent):void
{
     component2.component3.property1 = "Random data";
}

, !

+2

! , , - 5 , -)

.


  • Create your own event class with the necessary data that will be passed as members of this class (as shown here ).
  • Sending a special event from Component1

    dispatchEvent(new CustomEvent("customEvent",myData));
    
  • Create a method in Component2 that takes myData b> as an argument
  • Add an EventListener to the parent component and call the method in component2 passing event.myData strong>
0
source

Source: https://habr.com/ru/post/1707966/


All Articles