Clase para controlar los SharedObjects en Flash Lite 2.0

15 12 2006

Todos aquellos que hayáis utilizado un poco los SharedObjects de Flash Lite 2.0 habréis notado que son realmente incómodos a la hora de trabajar con ellos, así que he creado una clase que funciona con addEventListener. Se podría pulir mucho más, así que se aceptan sugerencias en el código.

La clase cuenta con tres métodos públicos.

init(nombre_del_archivo:String)
Inicializa el objeto SharedObject y devuelve un evento onInitSO que nos dice si el objeto existe o no.

loadSO()
Carga el objeto SharedObject.

saveSO(objeto)
Graba el objeto SharedObject. Hay que pasarle un objeto Flash con todos los valores dentro y la clase se encarga de parsear el objeto.

Código de la clase

Actionscript:
  1. import utils.*;
  2.  
  3. /**
  4. * @author Raul Jimenez
  5. */
  6. class utils.SharedObjectLite extends MovieClip
  7. {
  8.     private var dispatchEvent:Function;
  9.     public var addEventListener:Function;
  10.     public var removeEventListener:Function;
  11.     public var removeAllEventListeners:Function;
  12.    
  13.     private var object_so:SharedObject;
  14.     private var wait_int:Number;
  15.    
  16.     static var ON_INIT_SO = "onInitSO";
  17.     static var ON_SAVE_SO = "onSaveSO";
  18.     static var ON_LOAD_SO = "onLoadSO";
  19.    
  20.     /**
  21.     * @since 01-nov-2006
  22.     * @author Raul Jimenez
  23.     *
  24.     * @usage
  25.     * Constructor
  26.     */
  27.     public function SharedObjectLite()
  28.     {
  29.         GDispatcher.initialize(this);
  30.        
  31.         object_so = new SharedObject();
  32.        
  33.     }
  34.    
  35.     /**
  36.     * @author Raul Jimenez
  37.     * @since 15-dic-2006
  38.     *
  39.     * @usage
  40.     *
  41.     */
  42.     public function init(name_str:String):Void
  43.     {
  44.         trace("init()");
  45.        
  46.         SharedObject.addListener(name_str, onLoad);
  47.         object_so = SharedObject.getLocal(name_str);
  48.         object_so._scope = this;
  49.     }
  50.    
  51.     /**
  52.     * @author Raul Jimenez
  53.     * @since 15-dic-2006
  54.     *
  55.     * @usage
  56.     *
  57.     */
  58.     private function onLoad(data_so:SharedObject):Void
  59.     {
  60.         trace("onLoad(): " + data_so.getSize());
  61.        
  62.         var exists_bol:Boolean = new Boolean();
  63.        
  64.         if (data_so.getSize() != 0)
  65.         {
  66.             exists_bol =  true;
  67.         }
  68.         else
  69.         {
  70.             exists_bol =  false;
  71.         }
  72.        
  73.         data_so._scope.dispatchEvent({type:ON_INIT_SO, target:exists_bol});
  74.     }
  75.    
  76.     /**
  77.     * @author Raul Jimenez
  78.     * @since 15-dic-2006
  79.     *
  80.     * @usage
  81.     *
  82.     */
  83.     public function loadSO():Void
  84.     {
  85.         trace("loadSO()");
  86.        
  87.         dispatchEvent({type:ON_LOAD_SO, target:object_so});
  88.     }
  89.    
  90.     /**
  91.     * @author Raul Jimenez
  92.     * @since 15-dic-2006
  93.     *
  94.     * @usage
  95.     *
  96.     */
  97.     public function saveSO(data_obj:Object):Void
  98.     {
  99.         trace("saveSO()");
  100.        
  101.         for (var i in data_obj)
  102.         {
  103.             object_so.data[i] = data_obj[i];
  104.         }
  105.        
  106.         object_so.data.initialized = true;
  107.         object_so.flush();
  108.                
  109.        
  110.         dispatchEvent({type:ON_SAVE_SO, target:"ok"});
  111.     }
  112. }

También he creado un ejemplo por si alguien tenía dudas.

Descargar ejemplo de la clase SharedObjectLite



Acciones

Informacion

5 respuestas a “Clase para controlar los SharedObjects en Flash Lite 2.0”

18 12 2006
Alessandro (04:46:17) :

Ciao

really cool class. Just a comment on using class, developers must understand that defining classes adds has some memory overhead. So the suggestions is to keep an eye on class utilization.

Alessandro

18 12 2006
Raul Jimenez (15:15:33) :

Hi Alessandro!

Thank you for the suggestion.

Do you know where can i find some information about good techniques of classes in Flash Lite?

Im working with classes usually and it can be very useful.

Bye!

19 12 2006
20 12 2006
Raul Jimenez (09:17:58) :

Recuerdo haber leído ese PDF hace un tiempo, te da una buena idea de como hacer bien las cosas.

Aunque desgraciadamente no dice nada del tema de las clases :(

Deu!

21 12 2006
Marcos (00:12:01) :

Cierto me lo imprimí ayer y no dice nada de ese tema, en cierto modo es normal si se considera que no recomiendan situar mucho codigo en un mismo frame. Normalmente yo empleo clases para gestión de aspectos generales y el codigo “funcional” de cada pantalla lo aplico en cada frame para repartirlo lo mejor posible. Hasta la fecha no he tenido problemas de rendimiento, pero tampoco he realizado proyectos que requieren un uso de procesador importante.

Deje un comentario

usted puede usar estos tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>