2009-07-09 85 views
2

我有一个Flex应用程序,我正在为一项新工作而努力。这是一种训练轮的应用程序 - 我正在学习语言,这不是一个需要与服务交流才能完成工作的应用程序。在整个应用程序中有几个组合框的实例共享相同的一组可能的值(比如,选择状态:“进行中”,“被拒绝”,“完成”),我想要使用相同的数据源。Flex中组件之间共享数据的最佳方式是什么?

什么是最好的管理方式?

回答

3

MVC架构....以及在简单的情况下只是Model部分:

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; 
    } 

} 

然后你就可以更新和任何这样的组件使用Singleton:

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

组件1

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

组分2

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

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

0

只是将它们初始化为父组件中的一个数组。

+0

假设他们并不都共享一个共同的父组件? – 2009-07-09 15:56:44

相关问题