Pass data between flex components

前端 未结 2 1348
情深已故
情深已故 2021-02-11 01:31

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 on

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-11 02:31

    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.

    
    
    

    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.

提交回复
热议问题