tags:

views:

25

answers:

4

i have in my flex application various mxml components that all need to show stats based on the same data. how can i go about doing that? do i need some mvc framework like cairngrom or puremvc for that or can i do it without them? any design ideas?

A: 

You don't need any framework for that. Do you know about data-binding?

http://www.flexafterdark.com/docs/Flex-Binding

This way you can set your data as dataprovider for many components. For example to show your data in dataGrid you set in mxml it's attribute

dataProvider="{yourDataArrayCollectionIdentifier}"

and in your arrayCollection declaration you need to set metatag [Bindable]

[Bindable] var yourDataArrayCollectionIdentifier : ArrayCollection;

there are other datatypes you can use as dataprovider, just arrayCollection is the most common

praksant
I know about data binding what I was asking was how to _share_ the data among different mxml views? Like a global array which is shared among different modules, only that I don't like global data. I was thinking about making a singleton class which will provide access to data and just wanted to see if there is any better solution/this has already been done before.
Do you mean how to get hold of that data from each component? You should pass that data into your component / module on it's creation. It's cleaner method than using singleton.
praksant
A: 

There are a handful of approaches to this. For encapsulation purposes, you should isolate your shared data out into a separate class; possibly a Value Object although it does't have to be.

Then create a public variable property in each MXML Component of this classes type. When you create the instance of each mxml component, pass in your 'global' instance of the data class.

You don't need to use an MVC framework, but the Cairngorm Model Locator could also be used to address this problem. The Model Locator is a singleton. You'd store your data inside the singleton instance; and then each MXML Component would reference the singleton for the data. Creating an external dependency like this breaks encapsulation, though. I much prefer the parameter passing route for non-application specific components.

www.Flextras.com
A: 

Tour de Flex now has a section on frameworks that points to the various options that are out there. Also check out a video I did on Flex Best Practices. And check out the First Steps in Flex Screencast on Architectural Patterns.

James Ward
A: 
package 
{
    public class ApplicationViewModel
    {
        [Bindable] public var message:String = "";  
    }
}

You can now use this message across your MXML where you make the instance of that.

A singleton class is used in different scenarios where you want to hold some information of all states. A better example would be Chess Board, where your board is Singleton class and its state should never change as you have to keep track of all coins moved across the board and its position.

You are injecting this message variable in the views where you want to show the data.

Vinothbabu