15 Replies Latest reply on Feb 6, 2011 3:55 AM by Pablo Souza

    Value Objects not accessible

    bspShankar

      Hi,

      I am new to flex and have problem in accessing value objects.

       

      1. a video plays

      2. on completion of video display login page comes up

      3. On click of signin button i am stroing it in value objects through modellocator.

       


      ModelLocator

       

      package model
      {
          import vo.LoginVO;
         
          [Bindable]
          public class ModelLocator
          {
              private static var instance:ModelLocator=null;
             
              public static function getInstance():ModelLocator
              {
                  if (instance == null)
                      instance=new ModelLocator();
                  return instance;
              }
             
              public function ModelLocator()
              {
                  if (instance != null)
                  {
                      throw new Error("Cannot Create Instance Of Singleton Class ModelLocator");
                  }
                  instance=this;
                  initModels();
              }
             
              /*---------------------------------- VO Variables Declaration------------------- */
              public var login:LoginVO;
             
             
              /* -------------------------- VARIABLES INITIALIZATION-------------------------- */
              public function initModels():void
              {
                 
              }
          }
      }


      //value objects

       

      package vo
      {
          [Bindable]
          public class LoginVO
          {
              public var _username:String;
              public var _password:String;
             
             
              public function LoginVO()
              {
              }
              public function get username():String
              {
                  return _username;
              }
             
              public function set username(value:String):void
              {
                  _username = value;
              }
             
              public function get password():String
              {
                  return _password;
              }
             
              public function set password(value:String):void
              {
                  _password=value;
              }
          }
      }

       

       

      <s:states>

       

              <s:State name="State1"/>

       

              <s:State name="Carousel"/>

       

              <s:State name="LoginView"/>

       

              <s:State name="dispaly"/>

       

              <s:State name="test"/>

       

          </s:states>

       

       

       

          <local:Carousel includeIn="Carousel" />

       

          <Components:LoginView includeIn="LoginView" loginClickEvent="loginview1_loginClickEventHandler(event)"/>

       

          <Components:dispaly  includeIn="dispaly" clickEvent="currentState='test';"/>

       

          <Components:test includeIn="test"/>

       

          <mx:Canvas width="100%" height="100%" horizontalScrollPolicy="off" backgroundColor="#000000"

       

                     verticalScrollPolicy="off" includeIn="State1" >

       

              <mx:VideoDisplay id="videoDisplay" complete="currentState='LoginView';"       //

       

       

                               source="assets/AppFlv/NewFlutteringFlag.flv" x="410" y="179" />

       

          </mx:Canvas>

        • 1. Re: Value Objects not accessible
          bspShankar Level 1

          sorry i could not put my problem completely...

           

          cont..

           

          As mentioned above i have stored the value objectson click of of signin

           

          var loginvo:LoginVO=new LoginVO();

          loginvo.username=event.userName;

          loginvo.password=event.password;

           

          And i want to access username in other mxml page

           

          But iam not can please give me an hint

           

          Thanks and Regards

          • 2. Re: Value Objects not accessible
            Pablo Souza Level 3

            Are you having problems to populate your value object LoginVO?

             

            Please, send us more information.

             

             

            Regards,

            Pablo Souza

            • 3. Re: Value Objects not accessible
              Pablo Souza Level 3

              I got your problem now!

               

              You can have access to it through the ModelLocator instance.

               

              ModelLocator.getInstance()..loginVO._username;

              ModelLocator.getInstance()..loginVO._password;

               

              The ModelLocator is a implementation of the Singleton pattern, so that you assure you have just one instance of the ModelLocator class.

              You also have defined the getInstance() method as an static function. It allows you to retrieve an unique instance of the ModelLocator class from everywhere.

               

              Hope it helps you!

               

              Best regards,

              Pablo Souza

              • 4. Re: Value Objects not accessible
                bspShankar Level 1

                thanks for ur reply

                 

                 

                I have used it as below as u told.....

                <mx:VBox>
                        <s:Label text="Username"/>
                        <s:TextInput id="user" text="{modelLocator.login._username}"/>
                        <s:Label text="Password"/>
                        <s:TextInput id="pass" text="{modelLocator.login._password}"/>
                </mx:VBox>

                 

                I am getting the following error

                 

                but also iam not accessible

                i.e the text input is empty...

                 

                pls guide me

                 

                thanks & regards

                • 5. Re: Value Objects not accessible
                  bspShankar Level 1

                  /*---------------------------------- VO Variables Declaration------------------- */
                          public var login:LoginVO;

                   

                   

                  i.e i used

                   

                  <mx:VBox>
                          <s:Label text="Username"/>
                          <s:TextInput id="user" text="{modelLocator.login._username}"/>
                          <s:Label text="Password"/>
                          <s:TextInput id="pass" text="{modelLocator.login._password}"/>
                      </mx:VBox>

                  • 6. Re: Value Objects not accessible
                    Pablo Souza Level 3

                    As I told you the getInstance() method is returning the ModelLocator instance and it's an static method.

                    In order to retrieve the ModelLocator instance, you must call this static method and just after that you can have access to all your public variables.

                     

                    <mx:VBox>
                            <s:Label text="Username"/>
                            <s:TextInput id="user" text="{ModelLocator.getInstance().login._username}"/>
                            <s:Label text="Password"/>
                            <s:TextInput id="pass" text="{ModelLocator.getInstance().login._password}"/>
                    </mx:VBox>

                     

                     

                    I could see you were trying to get access to ModelLocator instance through a variable called modelLocator.

                    To do that you must have something like:

                     

                    private var modelLocator:ModelLocator = ModelLocator.getInstance();

                     

                     

                    Let mew know if you need more information.

                     

                     

                    Regards,

                    Pablo Souza

                    • 7. Re: Value Objects not accessible
                      bspShankar Level 1

                      yes i have used that in the below fashion

                       

                       

                      [Bindable]
                                  public var modelLocator:ModelLocator=ModelLocator.getInstance();

                       

                       

                      But also not able to access

                      • 8. Re: Value Objects not accessible
                        bspShankar Level 1

                        Is there any problem in populating the vo

                         

                        i have populated in the below fashion on click of sigin btn.

                         

                        var loginvo:LoginVO=new LoginVO();
                                        loginvo.username=event.userName;
                                        loginvo.password=event.password;

                         

                        Pls let me know if any mistake i have done

                         

                        Thanks & regards

                        • 9. Re: Value Objects not accessible
                          Pablo Souza Level 3

                          How did u assigned the username into login variable in ModelLocator class?

                           

                          You have sent this code:

                           

                          var loginvo:LoginVO=new LoginVO();
                          loginvo.username=event.userName;
                          loginvo.password=event.password;
                          

                           

                          After this lines, have you assigned the variable loginvo to the login variable in the ModelLocator class?

                           

                          var loginvo:LoginVO=new LoginVO();
                          loginvo.username=event.userName;
                          loginvo.password=event.password;
                          ModelLocator.getInstance().login = loginvo;
                          

                           

                          Regards,

                          Pablo Souza

                          • 10. Re: Value Objects not accessible
                            Pablo Souza Level 3

                            And also you don't need to create a new instance of loginVO.

                             

                            You can assign that directly just like below:

                             

                            ModelLocator.getInstance().login._username = event.userName;
                            ModelLocator.getInstance().login._password = event.password;
                            
                            

                             

                             

                            Regards,

                            Pablo Souza

                            • 11. Re: Value Objects not accessible
                              bspShankar Level 1

                              Thank u so much for ur support.....


                              Now its  working fine....

                               

                              With Regards

                              • 12. Re: Value Objects not accessible
                                bspShankar Level 1

                                Yeah Hi,...

                                 

                                if iam using the below way it works fine

                                 

                                var loginvo:LoginVO=new LoginVO();
                                loginvo.username=event.userName;
                                                loginvo.password=event.password;
                                                ModelLocator.getInstance().login = loginvo;
                                                currentState="dispaly";

                                 

                                But if i use  it directly like below i get error

                                 

                                ModelLocator.getInstance().login.username = event.userName;
                                                ModelLocator.getInstance().login.password = event.password;

                                 

                                 

                                error

                                 

                                TypeError: Error #1009: Cannot access a property or method of a null object reference.
                                    at NeuronLogitech_Lawpad/loginview1_loginClickEventHandler()[C:\Documents and Settings\xp\Adobe Flash Builder 4 Plug-in\NeuronLogitech_Lawpad\src\NeuronLogitech_Lawpad.mxml:26]
                                    at NeuronLogitech_Lawpad/___NeuronLogitech_Lawpad_LoginView1_loginClickEvent()[C:\Documents and Settings\xp\Adobe Flash Builder 4 Plug-in\NeuronLogitech_Lawpad\src\NeuronLogitech_Lawpad.mxml:45]
                                    at flash.events::EventDispatcher/dispatchEventFunction()
                                    at flash.events::EventDispatcher/dispatchEvent()
                                    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\co re\UIComponent.as:12266]
                                    at Components::LoginView/doLogin()[C:\Documents and Settings\xp\Adobe Flash Builder 4 Plug-in\NeuronLogitech_Lawpad\src\Components\LoginView.mxml:25]
                                    at Components::LoginView/__btnLogin_click()[C:\Documents and Settings\xp\Adobe Flash Builder 4 Plug-in\NeuronLogitech_Lawpad\src\Components\LoginView.mxml:56]

                                 

                                 

                                I just wanted why the second solution does not work in flex4

                                • 13. Re: Value Objects not accessible
                                  Pablo Souza Level 3

                                  That's because the loginVO instance is null inside the ModelLocator.

                                   

                                  If you wanna use this approache, initialize the login variable inside ModelLocator:

                                   

                                  public var login:LoginVO = new LoginVO();

                                   

                                   

                                  Regards,

                                  Pablo Souza

                                  1 person found this helpful
                                  • 14. Re: Value Objects not accessible
                                    bspShankar Level 1

                                    Hi,

                                     

                                    It worked fine......

                                     

                                    But i have doubt abt using getters and setters with value objects.....

                                     

                                    Is it necessary  to use it on client side........how beneficiary is it...

                                     

                                    Thanks & Regards

                                    • 15. Re: Value Objects not accessible
                                      Pablo Souza Level 3

                                      Hi,

                                       

                                      ActionScript supports properties, so usually you shouldn't ever need to declare it unless you are doing something out of the ordinary as trigger associated events, calculate a return value or wants to make it possible to override properties that are inherited from a superclass.

                                       

                                      Best regards,

                                      Pablo Souza