tags:

views:

199

answers:

2

I'm building my first flex app and am currently bussy splitting it up in multiple components to make it maintainable. I have a screen which holds a list that is displayed and filled after a succesfull login attempt:

Part of the main app:

<mx:ViewStack id="vsAdmin" height="100%" width="100%">
   <mx:TabNavigator id="adminTabs" width="100%" height="100%" historyManagementEnabled="false">
      <myComp:compBeheerdersAdmin id="beheerdersViewstackA"/>
   </mx:TabNavigator>
</mx:ViewStack>

In the component compBeheerdersAdmin there is a function requestBeheerdersList() that gets the data from the server and Binds it to the list through a handler.

After login the following code from the main app:

mainViewstack.selectedChild = vsAdmin;
//beheerdersViewstackA.createComponentsFromDescriptors();
beheerdersViewstackA.requestBeheerdersList();

The function requestBeheerdersList() does nothing (is not reached, i put a alert as first statement in the function but that is not displayed) when i login after a fresh load of the swf, but when i logout and login again, then the function is reached and the alert is displayed and the list is filled with the data from the server. Any ideas?

+1  A: 

I would make sure the component exists that you are calling before calling the next function. This could be done by forcing creationPolicy=all as you figured out. You could also add an event listener for the CreationComplete to call the function you want:

private function doThisFirst():void{
        mainViewstack.selectedChild = vsAdmin;
        vsAdmin.addEventListener(FlexEvent.CREATION_COMPLETE,doThis);
}


private function doThis():void{
        beheerdersViewstackA.requestBeheerdersList();
}

This may not be exactly correct but I tried to recreate to your specific example. If you are familiar with viewstack creation of its children and eventlisteners you should be able to fit this to your specific need.

Brandon
A: 

Alternatively, you can have creationComplete define in your mxml

<mx:Canvas ... creationComplete="onCreationComplete()">

<mx:Script>
  <![CDATA[
    private function onCreationComplete():void {
      requestBeheerdersList()
    }
 ]]>
</mx:Script>

or possibly

<mx:Canvas ... creationComplete="requestBeheerdersList()">

The difficulty with Flex is to understand how a mxml component maps the equivalent pure actionscript class. When you have in your mxml code something like <local:Mycomponent id="myComponent">, this add as child an instance of a class. The mxml file, Mycomponent.mxml, defines the class. Unless declared as static, the functions that are listed within the <mx:Script> tag are functions that apply on the instance. There is no constructor that you can explicitly define but the instance is not available before actual creation. You therefore have to rely on creationComplete to execute any function that you would have called from a constructor function in a strictly AS3 class.