您可以使用 SharedObject 物件的
data
屬性,將資料新增至
SharedObject
的 *.sol 檔案。若要將新的資料新增至共享物件,請使用下列語法:
sharedObject_name.data.variable = value;
下列範例會將
userName
、
itemNumbers
和
adminPrivileges
屬性及其值新增至 SharedObject:
public var currentUserName:String = "Reiner";
public var itemsArray:Array = new Array(101,346,483);
public var currentUserIsAdmin:Boolean = true;
mySO.data.userName = currentUserName;
mySO.data.itemNumbers = itemsArray;
mySO.data.adminPrivileges = currentUserIsAdmin;
在您將值指定給
data
屬性之後,必須指示 Flash Player 將那些值寫入 SharedObject 的檔案。若要強制 Flash Player 將值寫入 SharedObject 的檔案,請使用
SharedObject
.
flush()
方法,如下所示:
mySO.flush();
如果您未呼叫
SharedObject.flush()
方法,則 Flash Player 會在應用程式結束時將值寫入檔案。不過,如果資料超出預設設定,使用者就無法增加 Flash Player 儲存資料的可用空間。因此,最佳做法是呼叫
SharedObject.flush()
。
在使用
flush()
方法將共享物件寫入使用者的硬碟時,您必須仔細檢查使用者是否已經使用「Flash Player 設定管理員」(www.macromedia.com/support/documentation/tw/flashplayer/help/settings_manager07.html) 明確停用本機儲存功能,如下列範例所示:
var so:SharedObject = SharedObject.getLocal("test");
trace("Current SharedObject size is " + so.size + " bytes.");
so.flush();
在共享物件中儲存物件
您可以在 SharedObject 的
data
屬性中儲存簡單物件,例如 Arrays 或 Strings。
下列範例是 ActionScript 類別,可定義方法來控制與共享物件的互動。這些方法可讓使用者在共享物件中新增和移除物件。此類別會儲存內含簡單物件的 ArrayCollection。
package {
import mx.collections.ArrayCollection;
import flash.net.SharedObject;
public class LSOHandler {
private var mySO:SharedObject;
private var ac:ArrayCollection;
private var lsoType:String;
// The parameter is "feeds" or "sites".
public function LSOHandler(s:String) {
init(s);
}
private function init(s:String):void {
ac = new ArrayCollection();
lsoType = s;
mySO = SharedObject.getLocal(lsoType);
if (getObjects()) {
ac = getObjects();
}
}
public function getObjects():ArrayCollection {
return mySO.data[lsoType];
}
public function addObject(o:Object):void {
ac.addItem(o);
updateSharedObjects();
}
private function updateSharedObjects():void {
mySO.data[lsoType] = ac;
mySO.flush();
}
}
}
下列 Flex 應用程式會建立所需的每種共享物件類型的 ActionScript 類別實體。然後會在使用者新增或移除部落格或網站 URL 時,針對該類別呼叫方法。
<?xml version="1.0"?>
<!-- lsos/BlogAggregator.mxml -->
<mx:Application
xmlns:local="*"
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initApp()"
backgroundColor="#ffffff"
>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;
import flash.net.SharedObject;
[Bindable]
public var welcomeMessage:String;
[Bindable]
public var localFeeds:ArrayCollection = new ArrayCollection();
[Bindable]
public var localSites:ArrayCollection = new ArrayCollection();
public var lsofeeds:LSOHandler;
public var lsosites:LSOHandler;
private function initApp():void {
lsofeeds = new LSOHandler("feeds");
lsosites = new LSOHandler("sites");
if (lsofeeds.getObjects()) {
localFeeds = lsofeeds.getObjects();
}
if (lsosites.getObjects()) {
localSites = lsosites.getObjects();
}
}
// Adds a new feed to the feeds DataGrid.
private function addFeed():void {
// Construct an object you want to store in the
// LSO. This object can contain any number of fields.
var o:Object = {name:ti1.text, url:ti2.text, date:new Date()};
lsofeeds.addObject(o);
// Because the DataGrid's dataProvider property is
// bound to the ArrayCollection, Flex updates the
// DataGrid when you call this method.
localFeeds = lsofeeds.getObjects();
// Clear the text fields.
ti1.text = '';
ti2.text = '';
}
// Removes feeds from the feeds DataGrid.
private function removeFeed():void {
// Use a method of ArrayCollection to remove a feed.
// Because the DataGrid's dataProvider property is
// bound to the ArrayCollection, Flex updates the
// DataGrid when you call this method. You do not need
// to update it manually.
if (myFeedsGrid.selectedIndex > -1) {
localFeeds.removeItemAt(myFeedsGrid.selectedIndex);
}
}
private function addSite():void {
var o:Object = {name:ti3.text, date:new Date()};
lsosites.addObject(o);
localSites = lsosites.getObjects();
ti3.text = '';
}
private function removeSite():void {
if (mySitesGrid.selectedIndex > -1) {
localSites.removeItemAt(mySitesGrid.selectedIndex);
}
}
]]>
</mx:Script>
<mx:Label text="Blog aggregator" fontSize="28"/>
<mx:Panel title="Blogs">
<mx:Form id="blogForm">
<mx:HBox>
<mx:FormItem label="Name:">
<mx:TextInput id="ti1" width="100"/>
</mx:FormItem>
<mx:FormItem label="Location:">
<mx:TextInput id="ti2" width="300"/>
</mx:FormItem>
<mx:Button id="b1" label="Add Feed" click="addFeed()"/>
</mx:HBox>
<mx:FormItem label="Existing Feeds:">
<mx:DataGrid
id="myFeedsGrid"
dataProvider="{localFeeds}"
width="400"
/>
</mx:FormItem>
<mx:Button id="b2" label="Remove Feed" click="removeFeed()"/>
</mx:Form>
</mx:Panel>
<mx:Panel title="Sites">
<mx:Form id="siteForm">
<mx:HBox>
<mx:FormItem label="Site:">
<mx:TextInput id="ti3" width="400"/>
</mx:FormItem>
<mx:Button id="b3" label="Add Site" click="addSite()"/>
</mx:HBox>
<mx:FormItem label="Existing Sites:">
<mx:DataGrid
id="mySitesGrid"
dataProvider="{localSites}"
width="400"
/>
</mx:FormItem>
<mx:Button id="b4" label="Remove Site" click="removeSite()"/>
</mx:Form>
</mx:Panel>
</mx:Application>
在共享物件中儲存具有類型的物件
您可以在共享物件中儲存具有類型的 ActionScript 實體。作法是呼叫
flash.net.registerClassAlias()
方法來註冊類別。如果您建立類別的實體,並將它儲存於共享物件的資料成員中,之後再讀出該物件,便會取得具有類型的實體。SharedObject
objectEncoding
屬性預設支援 AMF3 編碼,並且會從 SharedObject 物件解壓縮儲存的實體;儲存的實體會保留您在呼叫
registerClassAlias()
方法時指定的相同類型。