What's the best way to share data between components in Flex?

痞子三分冷 提交于 2019-12-20 03:21:53

问题


I have a Flex application that I'm working on for a new job. It's sort of a training wheels application -- I'm learning the language, and this isn't an app that needs to talk to a service in order to do its job. There are a few instances of combo boxes throughout the application that share the same set of possible values (say, a selection of states: "In Progress", "Rejected", "Complete") that I want to have use the same data source.

What is the best way to manage this?


回答1:


MVC architecture ....well in simple cases just the Model part:

package 
{


    [Bindable]
    public final class ShellModelSingleton
    {   


        public var selectedStatus:ArrayCollection;




        ////////////////////////////////////////////
        // CONSTRUCTOR
        // ****DO NOT MODIFY BELOW THIS LINE*******
        /////////////////////////////////////////// 
        public function ShellModelSingleton(){}

        /****************************************************************
         * Singleton logic - this makes sure only 1 instance is created
         * Note: you are able to hack this since the constructor doesn't limit 
             * a single instance
         * so make sure the getInstance function is used instead of new 
             * ShellModelSingleton()
         *****************************************************************/ 
        public static function getInstance():ShellModelSingleton {
            if(_instance == null) {
                _instance = new ShellModelSingleton();
            }
            return _instance;
        }

        protected static var _instance:ShellModelSingleton;
    }

}

Then you can update and use the singleton from any component like this:

[Bindable] private var model:ShellModelSingleton = 
                              ShellModelSingleton.getInstance();

Component 1

<mx:DataGrid id="myDG" dataProvider="{model.selectedStatus}" />

Component 2

   <mx:List id="myList" dataProvider="{model.selectedStatus}" 
      labelField="label" />

Then any changes you make to the selectedStatus collection will be updated in both components.




回答2:


Just initialize them to an array in our parent component.



来源:https://stackoverflow.com/questions/1104701/whats-the-best-way-to-share-data-between-components-in-flex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!