2 Replies Latest reply on Aug 6, 2009 8:13 AM by MoonlightWare

    Passing VARs between MXML Application and MXML components!

    Vitor Wsis

      Hi,

       

      I'm trying to pass a variable between the MXML Application and the MXML Component with ValueObjects.

      But when i call the variable on the MXML Component it is null!

       

      ValueObject Class Code:

       

      package valueObjects
      {
          [Bindable]   
          public class MyGlobalVars
          {
              public var NomeGaleria: String;
             
              public function MyGlobalVars()
              {
              }
            
          }
      }

       

       

      MXML Application Code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  xmlns:comp="components.*" layout="vertical" height="100%" width="100%" >

       

      <mx:Script>
          <![CDATA[
              import valueObjects.MyGlobalVars;
              import mx.managers.PopUpManager;
              import mx.core.Container;
             
              [Bindable]
              public var nomeGaleria:MyGlobalVars;
                   


              private function AbreGaleria():void{
                    nomeGaleria=new MyGlobalVars();
                  nomeGaleria.NomeGaleria = "Moda";
                  PopUpManager.addPopUp(new galeriaImagens(),this,true);
              }
          ]]>
      </mx:Script>


      <mx:Panel height="673" width="839" verticalAlign="middle" borderStyle="none" layout="absolute">
       

          <mx:Canvas id="SMGaleria" width="815" height="30" x="2" y="98"">
              <mx:LinkButton x="474" y="5" label="moda" click="AbreGaleria()"/>   
           </mx:Canvas>
              
          <mx:ViewStack id="content" height="440" width="815" borderStyle="none" x="2" y="128">
              <comp:galeriaImagens id="GaleriaImagens" x="0" y="5" strGaleria="{nomeGaleria}"/>
          </mx:ViewStack>
       
      </mx:Panel>


      </mx:Application>

       

      MXML Component Code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:TitleWindow  xmlns:mx="http://www.adobe.com/2006/mxml"
          showCloseButton="true" creationComplete="CenterMe()" backgroundColor="#000000" xmlns:components="components.*" >


      <mx:Script>
          <![CDATA[
              import valueObjects.MyGlobalVars;
              import mx.controls.Alert;
       
                
              [Bindable]
              public var strGaleria:String;
         
              private function CenterMe(): void{

       

                   Alert.show(strGaleria);
       
                }          
          ]]>
      </mx:Script>


      </mx:TitleWindow>

       

      On the MXML component the value of strGaleria is null! It would not have to be: Moda??

        • 1. Re: Passing VARs between MXML Application and MXML components!
          Gregory Lafrance Level 6

          You may need to change your code as follows:

           

          <comp:galeriaImagens id="GaleriaImagens" x="0" y="5" strGaleria="{nomeGaleria.NomeGaleria}"/>

           

          Previously you had set strGaleria to the value of the object, not the string within the object.

           

          But I think you will still have a problem because function AbreGaleria will not be called until the user clicks the link button, and only then will NomeGaleria have a value, so I would set a default value as follows, otherwise at app launch it will be null:

           

          public var NomeGaleria: String = "";

          or  if that does not work

           

          public var NomeGaleria: String = " ";

          Another possible problem is that you are calling this code at the component creationComplete, but the LinkButton has not been clicked yet, so the value object string value has not been set, so the alert will not display anything:

           

          private function CenterMe():void{

              Alert.show(strGaleria);

          }

           

          I think you need to reorganize some things here.

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: Passing VARs between MXML Application and MXML components!
            MoonlightWare Level 1

            there are acutally a couple of way to accomplish this.

             

            1. convert valueObjects to a singleton object. http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postI d=262

             

            2. create a public function in the MXML component and after you open it call the function pass the value and do what ever you want in the function.

             

            Hope this helps