2009-07-09 82 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

相關問題