在Flex中的组件之间共享数据的最佳方法是什么?

时间:2021-05-23 20:58:25

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.

我有一个Flex应用程序,我正在为一份新工作而努力。它是一种训练轮应用程序 - 我正在学习该语言,而这不是一个需要与服务交谈以完成其工作的应用程序。整个应用程序中有一些组合框实例共享同一组可能的值(例如,选择状态:“正在进行”,“已拒绝”,“完成”)我想要使用相同的数据源。

What is the best way to manage this?

管理这个的最佳方法是什么?

2 个解决方案

#1


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

MVC架构....在简单的情况下只是模型部分:

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.

然后,您对selectedStatus集合所做的任何更改都将在两个组件中更新。

#2


Just initialize them to an array in our parent component.

只需将它们初始化为父组件中的数组即可。

#1


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

MVC架构....在简单的情况下只是模型部分:

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.

然后,您对selectedStatus集合所做的任何更改都将在两个组件中更新。

#2


Just initialize them to an array in our parent component.

只需将它们初始化为父组件中的数组即可。