11 Replies Latest reply on Feb 11, 2011 9:34 PM by sky858

    ShareObject help

    sky858

      Please forgive me if this answer should be obvious, but I really need your help,

      I have a flex project with a toggle button bar and a viewstack (id=viewstack1).  I want to save any information the user enters into the components within the viewstack so it is still there after the user closes and reopens the application.  So far I have...

       

      protected function button1_clickHandler(event:MouseEvent):void

           <![CDATA[

                protected function button1_clickHandler(event:MouseEvent):void

                {

                     var so:SharedObject = SharedObject.getLocal("FlashProject");

                     var storedObject:Object = so.data["viewstack1"];

       

                     so.data["viewstack1"] = viewstack1;

               }

           ]]>

       

      This should save the data (I think), but how do I load the data?  Do I need to add a flush somewhere?  I refrenced the save in the mxml application through the button click, but how do I tell it to load the sharedObject data on startup?  Do I create an .as file and tie it to the mxml app?  How do I refrence or tie it to the mxml?

      Thank you

        • 1. Re: ShareObject help
          Pablo Souza Level 3

          Hi,

           

          You must use the method flush() in order to write the shared object to the client file.

           

          In order to read the shared object add the method below in the application's creationComplete event:

           

          private var so:SharedObject;
          private function creationComplete_handler():void
          {
              so = SharedObject.getLocal("FlashProject");    
          }
          
          

           

           

           

          This way you can use the "so" variable to retrieve the written information:

           

          myComponent.text = so.data["viewstack1"].mySavedValue;
          

           

           

           

           

          Best regards,

          Pablo Souza

          1 person found this helpful
          • 2. Re: ShareObject help
            sky858 Level 1

            Thank you, I think I just about have it now.  The only problem I am having is

            with the last line,

             

            myComponent.text = so.data["viewstack1"].mySavedValue;

             

            where should I put that?  I tried putting it under the so=... in creationComplete, but I am getting errors.

            • 3. Re: ShareObject help
              Pablo Souza Level 3

              Hi,

               

              That line was just an example about how can you retrieve the information from the shared object.

              I need to know how you are writing the data to the shared object for know how you can retrieve it.

              It seems you are trying to write the ViewStack instance to the shared obejct and you cannot do that.

              You need to encapsulate the data from your component to an object and write this object to the shared object.

               

              The first step is to retrieving the shared object:

              private var so:SharedObject;
              private function creationComplete_handler():void
              {
                  so = SharedObject.getLocal("FlashProject");
                  if (so.size > 0)
                  {
                      
              viewstack1.loginComponent.firstName.text = so.data.firstName;
                      
              viewstack1.loginComponent.lastName.text = so.data.lastName;
                       viewstack1.loginComponent.email.text = so.data.email;
                  } 
              }

               

               

              You can write the data from your component to the shared object as below:

               

              protected function button1_clickHandler(event:MouseEvent):void 
              {
                  so.data.firstName =
              viewstack1.loginComponent.firstName.text;
                  so.data.lastName = viewstack1.loginComponent.lastName.text;
                  so.data.email = viewstack1.loginComponent.email.text;
              }

               

               

              Hope it helps you!

               

               

              Best regards,

              Pablo Souza

              • 4. Re: ShareObject help
                sky858 Level 1

                Thanks for the replies Pablo,

                I tried doing everything you said and it just isn't working for some reason.  Could you give me a simple, but detailed example?

                Lets say I have a viewstack with a textArea (id:g1) inside it.  I want to save the text that is entered by the user into the text area when a button is clicked and I want to load the information from the sharedObject when the application starts (I think with creationComplete).

                Thanks for all the help!

                • 5. Re: ShareObject help
                  Pablo Souza Level 3

                  Hi,

                   

                  That's what I meant. I've just tested and it works.

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
                                  layout="absolute" creationComplete="creationCompleteHandler(event)">
                      <mx:Script>
                          <![CDATA[
                              import mx.events.FlexEvent;
                              
                              private var so:SharedObject;
                              
                              protected function clickHandler(event:MouseEvent):void
                              {
                                  so.data.g1 = g1.text;
                              }
                  
                              protected function creationCompleteHandler(event:FlexEvent):void
                              {
                                  so = SharedObject.getLocal("FlashProject");    
                                  if (so.size > 0)    
                                  {         
                                      g1.text = so.data.g1;         
                                  }
                              }
                          ]]>
                      </mx:Script>
                  
                      <mx:ViewStack>
                          <mx:HBox>
                              <mx:TextInput id="g1" />
                              <mx:Button label="Save it" click="clickHandler(event)" />
                          </mx:HBox>
                      </mx:ViewStack>
                      
                  </mx:Application>
                  

                   

                   

                  Greets,

                  Pablo Souza

                  1 person found this helpful
                  • 6. Re: ShareObject help
                    sky858 Level 1

                    Thank you, now it works.  I am able to save everything in the components under the first button of my toggle button bar now.  The problem is I am not able to save anything under the second, third, fourth, etc button of the toggleButtonBar.  In the xml each button of the toggle button bar is reffered to as navigator content 1, 2, 3, 4, etc and the components are contained within each button.  If I try to save any component under any button except 1 it doesn't work.  Do I need to refer to these components in a way other than g2. text, etc?

                    Thank you for all the help

                    • 7. Re: ShareObject help
                      Pablo Souza Level 3

                      Hi,

                       

                      I haven't understood the problem. Please send the code, let's see if we can fix it.

                       

                      Greets,

                      Pablo Souza

                      • 8. Re: ShareObject help
                        sky858 Level 1

                        Sorry about that, here is a simplified version of the code with g1 and  t3 shown.  In the full code g1, t1, t2, cb1, y cb2 are all under  navigatorContent 1 and they all save perfectly, but t3 is under  navigatorContent 2.  t3 will not save even though I have it listed like  below.
                        <fx:Script>
                                <![CDATA[
                                    import mx.events.FlexEvent;
                                   
                                    private var so:SharedObject;
                                   
                                    protected function button1_clickHandler(event:MouseEvent):void
                                    {
                                        so.data.g1 = g1.text;
                                        so.data.t3 = t3.text;
                                        so.flush();
                                    }
                                ]]>
                            </fx:Script>
                           
                            <s:creationComplete>
                                <![CDATA[
                                so = SharedObject.getLocal("userData");
                                if (so.size > 0)
                                {
                                    g1.text = so.data.g1;
                                    t3.text = so.data.t3;
                                }
                            ]]>
                            </s:creationComplete>
                        <mx:Viewstack>
                            <s:NavigatorContent label="1">
                                    <s:BorderContainer>
                                        <s:TextArea id = "g1">
                                    <s:BorderContainer>
                            <s:NavigatorContent>
                            <s:NavigatorContent label="2">
                                    <s:BorderContainer>
                                        <s:TextArea id ="t3">
                                    <s:BorderContainer>
                            <s:NavigatorContent>
                        <mx:viewstack>

                        • 9. Re: ShareObject help
                          sky858 Level 1

                          Sorry about that, here is a simplified version of the code with g1 and t3 shown.  In the full code g1, t1, t2, cb1, y cb2 are all under navigatorContent 1 and they all save perfectly, but t3 is under navigatorContent 2.  t3 will not save even though I have it listed like below.

                          <fx:Script>

                                  <![CDATA[

                                      import mx.events.FlexEvent;

                                       

                                      private var so:SharedObject;

                                       

                                      protected function button1_clickHandler(event:MouseEvent):void

                                      {

                                          so.data.g1 = g1.text;

                                          so.data.t3 = t3.text;

                                          so.flush();

                                      }

                                  ]]>

                              </fx:Script>

                               

                              <s:creationComplete>

                                  <![CDATA[

                                  so = SharedObject.getLocal("userData");

                                  if (so.size > 0)

                                  {

                                      g1.text = so.data.g1;

                                      t3.text = so.data.t3;

                                  }

                              ]]>

                              </s:creationComplete>

                          <mx:Viewstack>

                              <s:NavigatorContent label="1">

                                      <s:BorderContainer>

                                          <s:TextArea id = "g1">

                                      <s:BorderContainer>

                              <s:NavigatorContent>

                              <s:NavigatorContent label="2">

                                      <s:BorderContainer>

                                          <s:TextArea id ="t3">

                                      <s:BorderContainer>

                              <s:NavigatorContent>

                          <mx:viewstack

                          • 10. Re: ShareObject help
                            Pablo Souza Level 3

                            Hi,

                             

                            I can't see where is your problem. I have developed an example with a viewstack and it's working.

                            Take a look at the code and try to understand qhat you are doing wrong in your code.

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <s:Application 
                             xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                xmlns:s="library://ns.adobe.com/flex/spark" 
                             xmlns:mx="library://ns.adobe.com/flex/mx"
                             creationComplete="creationCompleteHandler(event)">
                             
                             <fx:Script>
                              <![CDATA[
                               import mx.events.FlexEvent;
                               
                               private var sharedObject:SharedObject;
                               [Bindable]
                               private var text_g1:String;
                               [Bindable]
                               private var text_t3:String;
                               
                               protected function creationCompleteHandler(event:FlexEvent):void
                               {
                                sharedObject = SharedObject.getLocal("MyTextInformation");
                                if(sharedObject.size > 0)
                                {
                                 text_g1 = sharedObject.data.g1;
                                 text_t3 = sharedObject.data.t3;
                                }
                               }
                              
                               protected function saveClickHandler(event:MouseEvent):void
                               {
                                sharedObject.data.g1 = g1.text;
                                sharedObject.data.t3 = t3.text;
                                sharedObject.flush();
                               }
                              
                              
                               protected function clearClickHandler(event:MouseEvent):void
                               {
                                sharedObject.clear();
                               }
                             
                              ]]>
                             </fx:Script>
                             
                             <s:layout>
                              <s:VerticalLayout 
                               verticalAlign="middle"
                               horizontalAlign="center"/>
                             </s:layout>
                             
                             <s:ButtonBar dataProvider="{vs}" />
                             
                             <mx:ViewStack id="vs">
                              <s:NavigatorContent label="1">
                               <s:HGroup>
                                <s:Label text="g1:" />
                                <s:TextArea id = "g1" text="{text_g1}"/>
                               </s:HGroup>
                              </s:NavigatorContent>
                              <s:NavigatorContent label="2">
                               <s:HGroup>
                                <s:Label text="t3:" />
                                <s:TextArea id ="t3" text="{text_t3}"/>
                               </s:HGroup>
                              </s:NavigatorContent>
                             </mx:ViewStack>
                             <s:HGroup>
                              <s:Button label="Save" click="saveClickHandler(event)" />
                              <s:Button label="Clear" click="clearClickHandler(event)" />
                             </s:HGroup>
                             
                            </s:Application>
                            

                             

                             

                             

                             

                            Best regards,

                            Pablo Souza

                            • 11. Re: ShareObject help
                              sky858 Level 1

                              I got it to work a few times, but I am getting error 1009 now.  What is really odd is one time it will work perfectly and then another time the close button will not work and the debug shows error 1009 (Cannot acces a property or method of a null object reference). It just doesn't make any sense to me that it will work one time and not another even if there is no change to the code.  Any ideas?