views:

274

answers:

2

I have something like this

1.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
  <mx:Script source="_Public.as"/>
</mx:Application>

_public.as

[Bindable]
public var config:XML;  
public function init():void 
{
  var request:URLRequest = new URLRequest("config/config.xml");
  try {
    loader.load(request);
  } catch (error:Error) {
    trace("Unable to load requested document.");
  }
  loader.addEventListener(Event.COMPLETE,init_continue);
}

public function init_continue(event:Event):void {
  config =  new XML(loader.data);
}

The config is filled and I can use it. After that I press a button in 1.mxml and it creates 2.mxml popup (without the init).

config.xml

<data>
  <LOGIN_BUTTON>Inloggen</LOGIN_BUTTON>
</data>

Now I want to access config.LOGIN_BUTTON. But config doesn't give me anything.

Is it possible to get "access" to config, or not?

+1  A: 

First of all, always add a listener first, and issue load() only afterwards, like this:

  loader.addEventListener(Event.COMPLETE, init_continue);
  try {
    loader.load(request);
  } catch (error:Error) {
    trace("Unable to load requested document.");
  }

Otherwise the load might be fast just enough to skip your listener. Then, to your question. I'm unsure what you mean by "accessing" config.LOGIN_BUTTON. But if xml data is loaded successfully, the XML structure should be accessible as config.LOGIN_BUTTON, i.e. the latter should return an XMLList object. I'm afraid I need a clearer explanation of what you are trying to achieve to give a better answer than that.

Edit: based on your followup, I think it would be best to have a separate variable for the label, like this:

[Bindable]
private var buttonLabel:String;
....
public function init_continue(event:Event):void {
    config =  new XML(loader.data);
    buttonLabel = config.BUTTON_LABEL;
}

And then in 2.mxml:

<mx:FormItem label="{buttonLabel}" required="true"

I'm unsure if variable binding works inside an XML structure.

David Hanak
A: 

You can access it by using

Application.application.config.LOGIN_BUTTON
Chetan Sastry
Ok thanks, that works when I change 2.mxml into Application instead of Titlewindow. Is there an other way to keep 2.mxml a Titlewindow and to acces config.LOGIN_BUTTON?