Instance variable assignment in the declaration is always dangerous because dependencies are generally not tracked for initialization purposes.  Assigning the variable in a function that’s called later (like creationComplete or even initialize) is considered the best-practice.

 

Matt

 


From: [email protected] [mailto:[email protected]] On Behalf Of Ben Lucyk
Sent: Tuesday, December 06, 2005 1:03 PM
To: [email protected]
Subject: [flexcoders] Variable Initialization Bug? Data Binding Issues in Flex 1.5

 

OK, help me out here, 'cause I've been scratchin' my head a little too hard on this one...

 

What I'm trying to do is setup a simple static class to use as a pointer to my main, global model, from within my MXML components.  I've been running into some "interesting" behavior that I can't explain, and I'm wondering if anyone can help shed some light.   (Couldn't find much help in the docs or forums)

 

I'm seeing that when I'm at the <mx:Application> level and I try to bind to a "global" variable that is initialized from the static class I mentioned above, no binding takes place unless I initialize the var after creationComplete().  This is NOT required however, when I'm trying to use the same type of binding in a component.  (The var seems to initialize fine)

 

I should mention that I've also tested this using a "normal" class ( var resource:String = new com.StaticResource().test;), and also a singleton class ( var resource = com.StaticResource.getInstance().test;).  No difference.

 

Any thoughts?

 

 

<<<< Sample Code >>>>

 

<!-------------------- StaticResource.as ----------------------->


class com.StaticResource {
    public static var test:String = "Bliggidy!";
}  

 

<!------------------------------------------------------------------------>

 

 

 

<!-------------------- testStaticBinding.mxml  --------------->

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
     creationComplete="doCreated()">
     <mx:Script>
     <![CDATA[


      // Does not work here, only at "component" level!
      //var resource:String = com.StaticResource.test;
      var resource:String;
  
      function doCreated() {
           // Var must be set AFTER creationComplete in 
           // order to bind successfully!
           resource = com.StaticResource.test;
      }


     ]]>
     </mx:Script>
     <mx:HBox>
          <mx:Label text="Parent Level:" />
          <mx:Label text="{resource}" />
     </mx:HBox>
     <components:testStaticBindingComponent xmlns:components="components.*" />
</mx:Application>

 

<!------------------------------------------------------------------------>

 

 

<!------ testStaticBindingComponent.mxml--------------->

 

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.macromedia.com/2003/mxml">
    <mx:Script>
    <![CDATA[
     // Does NOT need to wait for creationComplete() in order to 
     // bind successfully!
     var componentResource:String = com.StaticResource.test;    //OK...
  
    ]]>
    </mx:Script>
    <mx:Label text="Component Level:" />
    <mx:Label text="{componentResource}" />
</mx:HBox>

 

<!------------------------------------------------------------------------>

 

 

Also, how are ModelLocators typically implemented using the Cairngorm framework?

Thanks in advance,
-Ben Lucyk
 




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




YAHOO! GROUPS LINKS




Reply via email to