tags:

views:

35

answers:

2

How do I change the application state in code, using a variable? when I provide a static string it works fine, but not with a variable.

For example, this works: (parent as mx.core.Application).currentState= 'history'

And this does not: (parent as mx.core.Application).currentState= @data

Yes, I know that @data is being populated, as I print it in an alert box. And yes, I have already tried "currentState = @data" and "currentState = '@data'. Sadly, they did not work.

Here is the menu object:

<mx:MenuBar id="mnuMain" labelField="@label" itemClick="menuHandler(event);">
    <mx:XMLList>
        <menuitem label="File">
            <menuitem label="Exit" data="exit" />
        </menuitem>
        <menuitem label="View">
            <menuitem label="Home" data="home" />
            <menuitem label="Monitor" data="impmon" />
            <menuitem label="History" data="history" />
            <menuitem label="Tables" data="tables" />
            <menuitem label="Schema View" data="schema" />              
        </menuitem>
    </mx:XMLList>
</mx:MenuBar>

Here is the handler:

  private function menuHandler(event:MenuEvent):void {
var newstate:String;
newstate = @data.toString();        
(parent as mx.core.Application).currentState = newstate;
  }
A: 

I am guessing you are getting this from an XML, try [email protected]()

Hope that helps.

EDIT: The mxml should look something like the below and it should work fine with event.item.@data

<mx:MenuBar id="mnuMain" labelField="@label" itemClick="menuHandler(event);" showRoot="false">
    <mx:dataProvider>
       <mx:XMLList>
            <menu>
               <menuitem label="test">
                <menuitem label="History" data="history" />
               </menuitem>
            </menu>
       </mx:XMLList>
    </mx:dataProvider>
</mx:MenuBar>
JustFoo
Yes, @data is coming from an XML list.Tried your suggestion, it produced: "1120: access to an undefined property."Also tried: var newstate:String; newstate = @data.toString();This produced: "1178: Attempted access of inaccessible property data through a reference with static type flexmon." this was the same error I got originally when trying to change state.
I see posting code in a comment does not work well. I have added the menuBar object code, as well as the event handler to the main question.
JustFoo
WOW!! Changed assignment to: newstate = [email protected]();I got a really horrible and scary error message. It started with: "TypeError: Error #1009: Cannot access a property or method of a null object reference.", and I think one of the subsequent lines threatened to send someone to kill me if I tried that again.
Nope, got that scary error again, and now I think that Adobe has dispatched a team of killers to look for me.
<mx:dataProvider> node should be wrapping the xmllist
JustFoo
The dataProvider element tags are wrapping the XMLList tags.
A: 

Here's a quick example I whipped up:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:states>
        <mx:State name="exit">
            <mx:SetProperty target="{label1}" name="text" value="Exit State"/>
        </mx:State>
        <mx:State name="home">
            <mx:SetProperty target="{label1}" name="text" value="Home State"/>
        </mx:State>
        <mx:State name="impmon">
            <mx:SetProperty target="{label1}" name="text" value="Impmon State"/>
        </mx:State>
        <mx:State name="history">
            <mx:SetProperty target="{label1}" name="text" value="History State"/>
        </mx:State>
        <mx:State name="tables">
            <mx:SetProperty target="{label1}" name="text" value="Tables State"/>
        </mx:State>
        <mx:State name="schema">
            <mx:SetProperty target="{label1}" name="text" value="Schema State"/>
        </mx:State>
    </mx:states>

    <mx:Script>
        <![CDATA[
            import flash.utils.getQualifiedClassName;
            import mx.events.MenuEvent;

            private function menuHandler(event:MenuEvent):void 
            {
                trace("Clicked", String(event.item.@data));    
                currentState = String(event.item.@data);
            }
        ]]>
    </mx:Script>

    <mx:MenuBar id="mnuMain" labelField="@label" itemClick="menuHandler(event)">
    <mx:XMLList>
        <menuitem label="File">
            <menuitem label="Exit" data="exit" />
        </menuitem>
        <menuitem label="View">
            <menuitem label="Home" data="home" />
            <menuitem label="Monitor" data="impmon" />
            <menuitem label="History" data="history" />
            <menuitem label="Tables" data="tables" />
            <menuitem label="Schema View" data="schema" />              
        </menuitem>
    </mx:XMLList>
</mx:MenuBar>

<mx:Label horizontalCenter="0" verticalCenter="0" fontSize="30" fontWeight="bold" color="#ffffff" text="Default State" id="label1">
    <mx:filters>
        <mx:DropShadowFilter/>
    </mx:filters>
</mx:Label>

</mx:Application>
Sophistifunk