<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Calculator Tab &#187; SharedObject</title>
	<atom:link href="http://www.calculator-tab.com/blog/tag/sharedobject/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.calculator-tab.com</link>
	<description>Free Online Scientific Calculator</description>
	<lastBuildDate>Tue, 24 Aug 2010 19:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Accessing a Local Shared Object from multiple SWF files</title>
		<link>http://www.calculator-tab.com/blog/2008/08/accessing-a-local-shared-object-from-multiple-swf-files/</link>
		<comments>http://www.calculator-tab.com/blog/2008/08/accessing-a-local-shared-object-from-multiple-swf-files/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 20:12:45 +0000</pubDate>
		<dc:creator>Marek Kyncl</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[SharedObject]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.calculator-tab.com/?p=21</guid>
		<description><![CDATA[The setup Imagine you run a Flash calculator on your site, that can store numbers in a Local Shared Object (LSO), and have the great idea of adding a pop-up version of the calculator. After rolling up your brain sleeves and thinking about it for a minute, it becomes pretty clear that both versions will [...]]]></description>
			<content:encoded><![CDATA[<h3>The setup</h3>
<p>Imagine you run a Flash calculator on your site, that can store numbers in a Local Shared Object (LSO), and have the great idea of adding a pop-up version of the calculator. After rolling up your brain sleeves and thinking about it for a minute, it becomes pretty clear that both versions will have to access the same LSO. There are mainly two reasons for this:</p>
<ol>
<li>In the pop-up version the user will want to have access to the numbers she has already stored in the main version.</li>
<li>Numbers stored in the pop-up version should become available to the main version.</li>
</ol>
<p>&#8220;That&#8217;s an easy one&#8221;, you think. &#8220;Since I&#8217;m already using the
<pre><code>SharedObject.flush()</code></pre>
<p> method every time I save a number, to make sure it gets written to the disk, the LSO will always be up-to-date. Right?&#8221;</p>
<p>You guessed it &#8230;</p>
<h3>The problem</h3>
<p>Wrong. The following scenario demonstrates it: A visitor goes to the website, makes some calculations and saves a few numbers. He then opens the pop-up version, it loads the numbers from the LSO, and he saves a few more numbers. He closes the pop-up and the LSO now contains the old numbers from the main version and the new numbers from the pop-up version. So far so good.</p>
<p>But now he goes to watch the latest episode of Diggnation and that&#8217;s where the problem starts. Alex and Kevin (once again) start trying to figure out how many beers they must have drank in all the Diggnation episodes. Our visitor thinks &#8220;Ah, wait a minute, I have the calculator still open. I&#8217;ll figure it out once and for all&#8221;. He clicks on the calculator tab and starts crunching the numbers. When he&#8217;s done he saves the result, so that he can email it to them, because they <em>couldn&#8217;t</em> figure it out. The problem is, that now the LSO contains the old numbers from the main version and the number of beers that Kevin and Alex drank &#8211; but the saved numbers from the pop-up are gone.</p>
<p>The reason this happens, is that once you load the LSO with the
<pre><code>SharedObject.getLocal("lsoID_str", "/")</code></pre>
<p> method into an SWF file, the SWF file works with it&#8217;s own temporary copy of the LSO. Whatever happens with the real LSO in the meantime, it will be overwritten by the SWF&#8217;s own copy the next time
<pre><code>SharedObject.flush()</code></pre>
<p> is called or when the SWF file flushes automatically by being closed.</p>
<h3>The solution</h3>
<p>That means that you need to force the SWF to update it&#8217;s copy of the LSO before you change any information in the LSO. The intuitive way to achieve this &#8211; calling the
<pre><code>SharedObject.getLocal("lsoID_str", "/")</code></pre>
<p> method again &#8211; doesn&#8217;t work. If the SWF already has a copy of the LSO, the
<pre><code>getLocal()</code></pre>
<p> method will not do anything. The way you <em>can</em> force the SWF to load a fresh copy of the LSO however, is by first deleting the variable to which you assigned the Shared Object.</p>
<p>So keeping in mind, that we have to refresh the LSO every time we want to access it, or store data in it, the right code for this job might look something like this:</p>
<pre><code>class SO {
     private var cookie_so:SharedObject;
     public function SO() {
     }
     public function saveNumber(id_str:String, value:Number):Void {
          loadSO();
          cookie_so.data[id_str] = value;
          unloadSO();
     }
     public function returnNumber(id_str:String):Number {
          loadSO();
          var fetchedNumber:Number = cookie_so.data[id_str];
          unloadSO();
          return fetchedNumber;
     }
     private function loadSO():Void {
          cookie_so = SharedObject.getLocal("calculator", "/");
     }
     private function unloadSO():Void {
          cookie_so.flush();
          delete cookie_so;
     }
}</code></pre>
<p>Now, you can open multiple instances of the SWF file, save numbers in each of them and LSO will contain the saved numbers from all of them.</p>
<h3>About the <code>getLocal()</code>method</h3>
<p>As you can see in the above script, the
<pre><code>SharedObject.getLocal("lsoID_str", "/")</code></pre>
<p> method has two parameters. The first one is required and holds the name of the LSO. If an LSO with this name doesn&#8217;t exist, this method will create one. Otherwise it loads the LSO into the variable it is assigned to.</p>
<p>The second parameter is optional and holds the &#8220;localPath&#8221;. This is important for our example, because if &#8220;localPath&#8221; is not specified, the LSOs of the calling SWFs will be stored in a folder structure mirroring the structure on the host. That means that if one calculator is in the root directory, it&#8217;s LSO will be stored in a folder called &#8220;example-domain.com&#8221;. But if the pop-up calculator is in a subfolder of the root directory called &#8220;pop-ups&#8221;, then it&#8217;s LSO will be stored in a subfolder of the &#8220;example-domain.com&#8221; folder called &#8220;pop-ups&#8221;. The result would be, that the root calculator would not be able to read the LSO of the pop-up calculator. That is why we need to specify the second parameter. The value of &#8220;/&#8221; specifies, that the LSO is stored under &#8220;example-domain.com&#8221; to which every SWF running on that domain has access.</p>
<h3>Notes</h3>
<p>This solution works reliably when the SWF files are embedded in a browser, but it works only sporadically when I open the files locally in my Flash player. I suspect this is more a problem with my system, rather then with the script, because when I open the same files locally with a browser, they work fine again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.calculator-tab.com/blog/2008/08/accessing-a-local-shared-object-from-multiple-swf-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
