logo
  • Home
  • Acerca
  • Autores
  • Faq
  • Rede
  Twitter   Feed-me! RSS!

Flex tip: Understanding the SharedObject

Colocado por Pablo Souza na(s) categoria(s): .NET, 1, 2009, action, Actionscript, Adobe, AMF, app, AR, BI, boolean, browser, case, class, encode, event, events, flash, Flash Player, Flex, for, Formação, function, handle, html, if, int, label, layout, library, live, MXML, Number, O, on, platform, player, pt, reference, Ria’s Geral, spark, string, try, Ved, XML em 02 16th, 2011 | Sem comentários

Today the Flex tip will talk about the usage of the local SharedObject object and how to implement it to store simple data objects.
When you create a SharedObject obejct the Flash Player also creates an empty *.sol file that stores the SharedObject data limited up to 100 KB per domain. If you try to saving bigger data the user must to authorize this process through a Local Storage dialog box. The supported types are: Number, String, Boolean, XML, Date, Array and Objects. You can also store typed ActionScript instances by callling the flash.net.registerClassAlias() function in order to preserve the class type of an object when the object is encoded in AMF.

Use the SharedObject.getLocal() method for create a SharedObject object:

SharedObject.getLocal("chooseSomeName"): SharedObject

For instance:

var sharedObject:SharedObject;
sharedObject = SharedObject.getLocal("MyTextInformation");

You can also create multiple SharedObjects for the same Flex application:

sharedObject1 = SharedObject.getLocal("UserInformation");
sharedObject2 = SharedObject.getLocal("TravelInformation");
sharedObject3 = SharedObject.getLocal("AgreementInformation");

The code above will create 3 files in the Flex folder:

UserInformation.sol
TravelInformation.sol
AgreementInformation.sol

In order to store these information into a SharedObject you must use the property data:

sharedObject.data.information = "the information goes here";

The method flush() causes Flash Player to write the information in the respective data file. In case you don’t call it directly, the data will be persisted once you close the browser. Therefore, it’s a good practice to call it.

sharedObject.flush();

The clear() method clears the file and removes it from the local disk.

sharedObject.clear();

Check the complete example out:

<?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;
?
	protected function creationCompleteHandler(event:FlexEvent):void
	{
		sharedObject = SharedObject.getLocal("MyTextInformation");
		if(sharedObject.size > 0)
		{
			information.text = sharedObject.data.information;
			currentDate.text = "Eu gravei essa data no objeto: " +
				sharedObject.data.currentDate;
		}
	}
?
	protected function saveClickHandler(event:MouseEvent):void
	{
		sharedObject.data.information = information.text;
		sharedObject.data.currentDate = new Date();
		sharedObject.flush();
	}
?
?
	protected function clearClickHandler(event:MouseEvent):void
	{
		sharedObject.clear();
	}
?
	]]>
fx:Script>
?
<s:layout>
	<s:VerticalLayout
		verticalAlign="middle"
		horizontalAlign="center"/>
s:layout>
?
<s:Label id="currentDate" />
<s:TextArea id="information" />
<s:HGroup>
	<s:Button label="Salvar informação"
			  click="saveClickHandler(event)" />
	<s:Button label="Apagar informação"
			  click="clearClickHandler(event)" />
s:HGroup>
?
s:Application>

References:
http://livedocs.adobe.com/flex/3/html/help.html?content=lsos_5.html

Hope you enjoy it!



Veja o post original no blog do autor aqui!  

Pablo Souza

Escrito por Pablo Souza @ http://rectius.com.br/blog
Saiba mais sobre o autor na sua pagina de perfil
Outros posts do autor:
» Componente para Dashboard no Flex 4
» Dica Flex – Fixando a largura dos botões no Spark ButtonBar
» Dica Flex – Como embedar assets de um arquivo SWF

Deixe um comentário



Spam Protection by WP-SpamFree

ACERCA

O que é o RedeRIA ?

O redeRIA não é nada mais que um agregador de feed's que disponibiliza o conteudo de varios blogs e autores ao redor do mundo RIA, actualmente agregamos mais de 2791 entradas vindas de 53 blogs especializados em ria’s, pelo que só fica a ganhar em assinar o feed ou seguir a comunidade no twitter.

Se acha que o seu blog ou um blog de um amigo é interessante e util para os leitores o redeRIA, faça a sua submissão aqui.

Feed: assine já
Twitter: siga-nos

GOOGLE

Votação


Deveria o RedeRia agregar conteúdo em inglês?
Ver Resultados

AUTORES


Eduardo KrausAlexandre TadashiBindableCognitiva SoluçõesDaniel LopesDaniel SchmitzDanielPedrinhaDClick TeamEbercomEdgard DavidsonElvis FernandesErko BrideeFabiel PrestesFábio Batista da SilvaFabio da SilvaFabriccio BernardesFelipe BorellaFlavia MoreiraGabriel VersalliniGabriela T. PerryIgor MusardoJanderson CardosoJoão AugustoJose Carlos FielKelps SousaLeonardo FrançaLucas MarçalLuis MessiasLuiz TarabalMario JuniorMário SantosMauro MartinsPablo SouzaPedro ClaudioreneRia BrazilriaPTRicardo CerqueiraRobson FernandesRodrigo Pereira FragaSaintBrSamuelFacchinelloSergio SouzaSilva DeveloperStefan HorochovecTech CaffeTecinforThiago BuenoVedVinícius SandimWillian ManoXAML Cast

PUBLICIDADE








Powered by Wordpress & msdevstudio.com