1

I want to load an initial component to be displayed in my flex app, depending on whether a value in the SharedObject is set (first launch). How do I accomplish that?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
gurghet
  • 7,591
  • 4
  • 36
  • 63

1 Answers1

2

Easy enough:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               applicationComplete="onApplicationComplete()">
    <fx:Script>
        <![CDATA[
            private function onApplicationComplete():void
            {
                var so:SharedObject = SharedObject.getLocal('something');
                // add conditionals here
                    addElement(new SomeView());
            }
        ]]>
    </fx:Script>
</s:Application>

Or with States:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               applicationComplete="onApplicationComplete()">
    <fx:Script>
        <![CDATA[
            private function onApplicationComplete():void
            {
                var so:SharedObject = SharedObject.getLocal('something');
                // add conditionals here
                    this.currentState = 'someview2';
            }
        ]]>
    </fx:Script>
    <s:states>
        </s:State name="someview" />
        </s:State name="someview2" />
        </s:State name="someview3" />
    </s:states>

    <local:SomeView includeIn="someview" width="100%" height="100%" />
    <local:SomeView2 includeIn="someview2" width="100%" height="100%" />
    <local:SomeView3 includeIn="someview3" width="100%" height="100%" />
</s:Application>
J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • I’m sorry but I thought it had something to do with the viewStack. Is this good practice? – gurghet May 26 '11 at 17:31
  • You could use ViewStack if you wanted to but I would use states instead since ViewStack is getting deprecated. Modified answer. – J_A_X May 26 '11 at 17:33
  • Deprecated? Could you please elaborate on that? ViewStacks are an entire chapter of the Flex book I bought. – gurghet May 26 '11 at 17:50
  • ViewStacks are Flex 3 (mx package), we are now in Flex 4 (Spark) and there has been a *major* fix in states for Flex 4. Hence, there's no need to use ViewStack, just use states. I think your book might be a tad old and aiming for Flex 3, but it's still not a bad idea to learn about deffered instantiation. – J_A_X May 26 '11 at 17:57