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

后端 未结 2 1198
不思量自难忘°
不思量自难忘° 2021-01-22 01:51

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 ta

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 02:15

    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

    
    

    Component 2

       
    

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

提交回复
热议问题