tags:

views:

29

answers:

1

What would be the best way to keep a value throughout different tabs.

I have a main page which has several tabs, each tab is opened as a component. And I need to have a value for ID to be preserved thru each tab.

Right now in the main page I declare the value

    [Bindable]
    public var myID:String;

And then call it using;

     parentApplication.myID

Is there a better/more efficient way?

+3  A: 

Accessing the parentApplication breaks encapsulation; and "calling up" is generally considered a bad practice [by me at least].

I might give Each component a "myID" variable, like this:

[Bindable] public var myID : String;

Then pass that ID into each component:

<comp:MyComponent myID="{GlobalID}" />

If the ID Changes in one spot, Binding should update it elsewhere.

The alternative, which is used by a lot of frameworks, is to create a singleton object. Store the myID variable in that singleton object and have each component access that singleton object. The Cairngorm ModelLocator is a good singleton sample, but others exist.

As with the 'parentApplication.myID' approach, a singleton adds an external dependency to your component which breaks encapsulation and minimizes reuse. Using one is not a decision I would make lightly, but there are always trade offs.

www.Flextras.com