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

I have a Flex application that I'm working on for a new job. This is a kind of application for training wheels - I'm learning a language, and this is not an application that needs to talk to the service in order to do its job. There are several instances of combo boxes in the application that have the same set of possible values โ€‹โ€‹(for example, the choice of states: โ€œRunningโ€, โ€œRejectedโ€, โ€œFullโ€) that I want to use the same data source.

What is the best way to manage this?

+2
flex
source share
2 answers

MVC architecture .... well, in simple cases, only part of the 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; } } 

Then you can update and use singleton from any component as follows:

 [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.

+3
source share

Just initialize them into an array in our parent component.

0
source share

All Articles