2 Replies Latest reply on May 18, 2011 9:53 PM by snkd

    Session in Flex

    snkd

      You can Use the following class as a session in flex..

       

      [Bindable]
      private var session:Session = Session.getInstance();

       

      How to insert data to session

      session.setAttribute('username','sanka')

       

      How to retrive data from session

      var userName:String=session.getAttribute('username');

       

       

       

      http://dl.dropbox.com/u/7375335/Session.as

       

      package model
      {

       

         
          import flash.utils.Dictionary;

       


          [Bindable]
          /**
           * @author Sanka Senavirathna
           *
           *<p>
           *    Conforms to Singleton Design Pattern.
           *     </p>
           *    <p>
           *        Session.getInstance().getAttribute(key)
           *        Session.getInstance().setAttribute(key,value)
           * Code example for client
           [Bindable]
           private var session:Session = Session.getInstance();
           session.getAttribute('name')
           *    </p>
           * */
          public class Session
          {
              private static var instance:Session = new Session();
              private var dic:Dictionary=new Dictionary(); // keep user session
             
             
              public function Session()
              {
                  if (instance != null) { throw new Error('Cannot create a new instance.  Must use Session.getInstance().') }
              }
             
             
              /**
               * This Session is a Dictionary that keep key value pairs.Key can be object and value can be object.
               * [Bindable]
               * private var session:Session = Session.getInstance();
               * @langversion ActionScript 3.0
               * @playerversion Flash 8
               * @see getAttribute
               * @see setAttribute
               * @author Sanka Senavirathna
               */
              public static function getInstance():Session
              {
                  return instance;
              }

       

             
              /**
               *
               * [Bindable]
               * private var session:Session = Session.getInstance();
               * Session.getInstance().setAttribute(key,value)
               * <p>example
               * session.setAttribute('username','sanka')
               *</p>
               * @langversion ActionScript 3.0
               * @playerversion Flash 8
               *
               * @param key Describe key here.
               * @param value Describe value here.
               *
               * @see getAttribute
               * @author Sanka Senavirathna
               */
              public function setAttribute(key:*,value:*):void
              {
                  dic[key]=value;
              }
             
              /**
               * [Bindable]
               * private var session:Session = Session.getInstance();
               * Session.getInstance().getAttribute(key)
               * <p>example
               * var userName:String=Session.getInstance().getAttribute('username');
               *</p>
               * @langversion ActionScript 3.0
               * @playerversion Flash 8
               *
               * @param param1 Describe key here.
               *
               * @see setAttribute
               * @author Sanka Senavirathna
               */
              public function getAttribute(key:*):*
              {
                  return dic[key];
              }
             
             
          }
      }

        • 1. Re: Session in Flex
          snkd Level 1

          I think This will help you to keep your shaired data in single place.this will optimize your program by

          1. reducing database hits,
          2. reducing Server hits

           

          Onece you retrive the data then put it into session (flex) and retrive it when you need..For example username in system.when user logged successfully

          put the user object in to session and hide the login component.then pop-up your other component.But you can access the user data from session by using

          var user:User=Session.getInstance().getAttribute('user'); something like that.

           

          Hope this will help you.

          enjoy

          happy cording