Log In  
  Getting Started  
----------------------------------------

Resources

 

Shared Objects

Shared objects are like browser cookies however

  • They do not expire by default.
  • By default, they are limited to a size of 100 KB each.
  • They can store simple data types (such as String, Array, and Date).
  • They are stored in a location specified by the application (within the user's home directory).
  • They are never transmitted between the client and server.
  • Use getLocal() to create a shared object. (SharedObject.getLocal("myTasks"); )
  • Use flush() to write the shared object to the client file. (sharedObj.flush())
  • Use clear() to destroy a shared object (sharedObj.clear())
previous page next page

User rating?

Shared Object Example

This example demonstrates the use of Shared Objects to save persistent data between application uses.

SharedObjectExample.MXML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	creationComplete="initSharedObject()">
	<mx:Script>
		<![CDATA[
		import flash.net.SharedObject;

		public var sharedObj:SharedObject;

	 	private function initSharedObject():void{
			sharedObj = SharedObject.getLocal("myTasks");
		    if (sharedObj.size > 0)
		    	textareaTasks.text=sharedObj.data.tasks;
		}

	 	public function localconnectionHandler(msg:String):void {
			textareaTasks.text= textareaTasks.text + msg + "\n";
	   	}

	   	private function clearTasks(event:MouseEvent):void {
	        textareaTasks.text="";
	   	}

	   	private function saveTasks(event:MouseEvent):void {
	        sharedObj.data.tasks = textareaTasks.text;
			sharedObj.flush();
	   	}

		private function deleteSavedTasks(event:MouseEvent):void {
		    sharedObj.clear();
		}

		]]>
	</mx:Script>
	<mx:Panel  horizontalCenter="0" verticalCenter="0.5" verticalGap="15"
		 paddingLeft="20" paddingRight="20" paddingBottom="20" paddingTop="20"
		  height="300">
		<mx:Label text="Your tasks are..."/>
		<mx:TextArea id="textareaTasks"
			 top="20" left="20" right="20" bottom="20"
			 width="100%" height="100%"/>
		<mx:HBox>
			<mx:Button id="btnClearTasks" click="clearTasks(event)"
				label="Clear Tasks"/>
			<mx:Button id="btnSaveTasks" click="saveTasks(event)"
				label="Save Tasks to Shared Object"/>
			<mx:Button id="btnDeleteSavedTasks" click="deleteSavedTasks(event)"
				label="Delete Saved Shared Object Tasks"/>
		</mx:HBox>
	</mx:Panel>
</mx:Application>

Rendered Example

Manually add a few Tasks then save them. After tasks are saved to the Shared Object, if you clear the tasks displayed and then refresh your browser you will see that the Flex application reads the local Shared object and repopulates the task list.

previous page next page

It seems that Flex saves the data in different locations for different browsers.

Try the sample above as follows:

1) run the sample in IExplorer and save tasks (e.g. "ie tasks").

2) open a new IE, and run the sample. You'll get "ie tasks".

3) open a new Firefox browser, and run the sample. You'll get blank tasks. Save tasks (e.g. "ff tasks").

4) open a new Firefox - you'll get "ff tasks". 

5) open a new IE - you'll get "ie tasks".

thanx,

hi,guy:

    There must be some wrong with you. I just try it: I enter the text "a" in environment FF and save it. And then I copy the url of the exaple page to environment IE,luckly,I can see the text "a" saved with FF.

    P.S: FF version: 3.0.10; IE version:7.0.5730.11

Yes, this is correct behaviour. Please recall, getLocal function is meant to be for the current client only. You will not see any shared data from several browser clients either of the same or different type. It's nothing to do with IE or FF, etc.

In order to accomplish the tasks you've mentioned please use getRemote instead. Alternatively you may still consider the use of getLocal for your scenario by refreshing the second browser every time the changes are saved in the first one or adding a new "ShowTaks" button with the code like this:
  sharedObj = null;
  sharedObj = SharedObject.getLocal("myTasks");
  textareaTasks.text = myData.data.tasks;

Hope it helps...

The data will be shared across some browsers. On my Mac, if I save a SharedObject in Safari, it shows up in Firefox, Camino, & OmniWeb.

I think the issue with IE and Firefox on Windows is that IE uses the ActiveX Flash plugin, and Firefox uses the Netscape Flash plugin, and apparently the different plugin types don't share their objects.

I bet if you opened it in Opera or Google Chrome, it would show the "ff tasks".

Can someone explain what is this one used for? Note that the example works fine without it.

public function localconnectionHandler(msg:String):void [
   textareaTasks.text= textareaTasks.text + msg + "\n";
]

Added by Mark Nichoson , last edited by matthew horn on Apr 22, 2008  (view change)
Labels: 
(None)

Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators