分享
 
 
 

一个openlaszlo使用flash的共享对象实现保存客户端信息的例子

王朝other·作者佚名  2006-11-20
窄屏简体版  字體: |||超大  

原文在这里:

http://forum.openlaszlo.org/showthread.php?p=25458#post25458

部分摘录如下:

-----------------------------------------------

localcache.lzx

-----------------------------------------------

<library>

<!---

* LocalCache

*

* A dictionary for localy cached data.

* It allows an application to maintain state or to enable the app

* to provide a user with an interface to create a list of keys

* and save and load data for each key,

* essentially creating a temporary local file store

* of xml documents or datasets

*

* We have APP_NAME and CACHE_VER constants

* that we use to version the keysets locally

* so that anytime that an app needs to make a change

* to the local datastructure it can bump up the version

* to avoid datastructure version problems between instances in the wild

* the appname is so that different apps from the same domain

* and which use the same code, don't step on each other's data

*

* expected use for maintaining application state

* // oninit

* if (gLocalCache.testWriteable())

* if(gLocalCache.checkLocalKey("mysavedstate"))...{

* mydata=gLocalCache.getLocalData("mydataname");

* // create LzDataElement with mydata

* } else ...{

* // create key and initialize with data

* gLocalCache.saveLocalKey("mydataname");

* gLocalCache.saveLocalData("mydataname", ds_state.serialize());

* }

* } else ...{

* // deal with the case where local data has been disabled

* }

*

-->

<!-- CONSTANTS -->

<script>

var APP_NAME ="app";

var CACHE_VER = "1";

</script>

<!-- CLASSES -->

<script>

<![CDATA[

//--------------------------------------------------------------------------------

// LocalCache Constructor

//--------------------------------------------------------------------------------

function LocalCache() ...{

// Debug.write("created");

}

LocalCache.prototype = new Object();

LocalCache.prototype.constructor = LocalCache;

Object.registerClass("LocalCache", LocalCache);

//--------------------------------------------------------------------------------

// testWriteable

//--------------------------------------------------------------------------------

LocalCache.prototype.testWriteable = function()...{

var retval = true;

var tstWriteObj = SharedObject.getLocal("tst");

if(!tstWriteObj.flush(10000)) ...{

// local cache is disabled

retval = false;

}

delete tstWriteObj;

return retval;

}

//--------------------------------------------------------------------------------

// saveLocalKeyDataPair

//--------------------------------------------------------------------------------

LocalCache.prototype.saveLocalKeyDataPair = function(key, data)...{

if (this.testWriteable())...{

if (this.checkLocalKey(key))...{

this.saveLocalData(key,data);

return "Saved the data, overwriting anything that was there";

} else ...{

this.saveLocalKey(key);

this.saveLocalData(key,data);

return "Created the key and saved the data.";

}

} else ...{

return "The user has disabled the local shared object";

}

}

//--------------------------------------------------------------------------------

// getLocalData

//--------------------------------------------------------------------------------

LocalCache.prototype.getLocalData = function(key)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

return localObj.data[key];

}

//--------------------------------------------------------------------------------

// getLocalKeys

//--------------------------------------------------------------------------------

LocalCache.prototype.getLocalKeys = function()...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

return localObj.data.keys;

}

//--------------------------------------------------------------------------------

// saveLocalKey

//--------------------------------------------------------------------------------

LocalCache.prototype.saveLocalKey = function(key)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

if(localObj.data.keys == null) ...{

localObj.data.keys = new Array(key);

} else ...{

if( this.checkLocalKey(key))...{

// then it already exists

} else ...{

localObj.data.keys.push(key);

localObj.data[key] = "";

}

}

localObj.flush();

delete localObj;

}

//--------------------------------------------------------------------------------

// checkLocalKey

//--------------------------------------------------------------------------------

LocalCache.prototype.checkLocalKey = function(key)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

var len = localObj.data.keys.length;

var retval = false;

for(i=0; i<len; i++)...{

if(localObj.data.keys[i] == key)...{

// it exists

retval = true;

}

}

return retval;

}

//--------------------------------------------------------------------------------

// deleteLocalKey

//--------------------------------------------------------------------------------

LocalCache.prototype.deleteLocalKey = function(key)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

var len = localObj.data.keys.length;

// delete the data if it exists first

this.deleteLocalData(key);

// then find and remove the key from the list

for(i=0; i<len; i++)...{

if(localObj.data.keys[i] == key)...{

localObj.data.keys.splice(i,1);

}

}

localObj.flush();

delete localObj;

}

//--------------------------------------------------------------------------------

// saveLocalData

//--------------------------------------------------------------------------------

LocalCache.prototype.saveLocalData = function(key, data)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

localObj.data[key] = data;

localObj.flush();

delete localObj;

}

//--------------------------------------------------------------------------------

// deleteLocalData

//--------------------------------------------------------------------------------

LocalCache.prototype.deleteLocalData = function(key)...{

var localObj = SharedObject.getLocal( APP_NAME+CACHE_VER);

delete localObj.data[key];

localObj.flush();

delete localObj;

}

]]>

</script>

<!-- INITIALIZATION -->

<script>

var gLocalCache = new LocalCache();

</script>

</library>

---------------------------------------------------------------

saveselectionsample.lzx

--------------------------------------------------------------

<canvas>

<debug y="700" width="750" height="175"/>

<include href="localcache.lzx"/>

<dataset name="dsselection1">

<items>

<item value="item1">item one</item>

<item value="item2">item two</item>

<item value="item3">item three</item>

<item value="item4">item four</item>

</items>

</dataset>

<dataset name="dsselection2">

<items>

<item value="item1">item one</item>

</items>

</dataset>

<view name="interface">

<simplelayout axis="y"/>

<view name="buttons" >

<wrappinglayout axis="y"/>

<button text="saveSelections" >

<method event="onclick">

<![CDATA[

var dp = dsselection1.getPointer();

// we should maintain a variable for the first check that fails and use that

// instead of calling checkLocalKey each time or the user will get the dialog box

// each time if they've disabled it and want to keep it disabled.

if(gLocalCache.checkLocalKey("dsselections"))...{

gLocalCache.saveLocalKeyDataPair("dsselections", dp.serialize());

} else ...{

// at this point local shared objects must be disabled or the key would exist

// from the initial call of loadSelections

// nevertheless we might want to reinitialize it here

// in case the user has changed their settings

// durring the session from disabled to enabled

// gLocalCache.saveLocalKeyDataPair("dsselections", dp.serialize());

}

]]>

</method>

</button>

<button text="loadSelections" >

<method event="onclick">

<![CDATA[

var dp = dsselection2.getPointer();

if(gLocalCache.checkLocalKey("dsselections"))...{

// Debug.write("localdata exits");

var selCacheNode = LzDataNode.stringToLzData(gLocalCache.getLocalData ("dsselections" ))

dsselection2.setChildNodes([selCacheNode.getFirstChild()]);

// Debug.write("loaded and serialized:"+ dp.serialize());

} else ...{

// Debug.write("--in-else-lso-needs-initializing--" );

gLocalCache.saveLocalKeyDataPair("dsselections", dp.serialize());

}

]]>

</method>

</button>

</view>

<combobox id="gogocombo1" editable="false">

<textlistitem name="textguy1"

datapath="dsselection1:/items/item"

text="gogo" />

</combobox>

<combobox id="gogocombo2" editable="false">

<textlistitem name="textguy2"

datapath="dsselection2:/items/item"

text="gogo gadget" />

</combobox>

</view>

</canvas>

该例子后面有评价认为,如果有多个用户同时使用一个browser,那么他们有可能共享相同的shareobject,除非在shareobject中区分用户,而且这个临时数据在某一个用户清空缓存的时候清掉所有的临时数据,所以还是局限性。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有