flex sharing data between different components

2019-08-14 03:52发布

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?

4条回答
祖国的老花朵
2楼-- · 2019-08-14 04:09

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

查看更多
乱世女痞
3楼-- · 2019-08-14 04:17

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.

查看更多
老娘就宠你
4楼-- · 2019-08-14 04:26

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.

查看更多
【Aperson】
5楼-- · 2019-08-14 04:26
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.

查看更多
登录 后发表回答