2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
(function () {
|
2014-08-25 23:49:01 +08:00
|
|
|
|
|
|
|
'use strict';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
|
|
|
var
|
2014-08-25 23:49:01 +08:00
|
|
|
window = require('window'),
|
|
|
|
JSON = require('JSON'),
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
Consts = require('Common/Consts'),
|
|
|
|
Utils = require('Common/Utils')
|
2013-11-16 06:21:12 +08:00
|
|
|
;
|
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-01-27 05:06:00 +08:00
|
|
|
function LocalStorageDriver()
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-02 19:34:17 +08:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2015-01-27 05:06:00 +08:00
|
|
|
LocalStorageDriver.supported = function ()
|
2014-08-20 23:03:12 +08:00
|
|
|
{
|
|
|
|
return !!window.localStorage;
|
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-20 23:03:12 +08:00
|
|
|
/**
|
|
|
|
* @param {string} sKey
|
|
|
|
* @param {*} mData
|
2014-09-02 19:34:17 +08:00
|
|
|
* @return {boolean}
|
2014-08-20 23:03:12 +08:00
|
|
|
*/
|
2015-01-27 05:06:00 +08:00
|
|
|
LocalStorageDriver.prototype.set = function (sKey, mData)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-20 23:03:12 +08:00
|
|
|
var
|
2014-09-02 19:34:17 +08:00
|
|
|
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
|
2014-08-20 23:03:12 +08:00
|
|
|
bResult = false,
|
|
|
|
mResult = null
|
|
|
|
;
|
|
|
|
|
|
|
|
try
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-09-02 19:34:17 +08:00
|
|
|
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
|
|
|
|
}
|
2015-10-14 00:36:43 +08:00
|
|
|
catch (e) {}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
2014-09-02 19:34:17 +08:00
|
|
|
if (!mResult)
|
|
|
|
{
|
|
|
|
mResult = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
mResult[sKey] = mData;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
window.localStorage[Consts.Values.ClientSideStorageIndexName] = JSON.stringify(mResult);
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
bResult = true;
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2015-10-14 00:36:43 +08:00
|
|
|
catch (e) {}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
return bResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} sKey
|
2014-09-02 19:34:17 +08:00
|
|
|
* @return {*}
|
2014-08-20 23:03:12 +08:00
|
|
|
*/
|
2015-01-27 05:06:00 +08:00
|
|
|
LocalStorageDriver.prototype.get = function (sKey)
|
2014-08-20 23:03:12 +08:00
|
|
|
{
|
|
|
|
var
|
2014-09-02 19:34:17 +08:00
|
|
|
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
|
2014-08-20 23:03:12 +08:00
|
|
|
mResult = null
|
|
|
|
;
|
|
|
|
|
|
|
|
try
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-09-02 19:34:17 +08:00
|
|
|
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
|
2014-08-20 23:03:12 +08:00
|
|
|
if (mResult && !Utils.isUnd(mResult[sKey]))
|
|
|
|
{
|
|
|
|
mResult = mResult[sKey];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mResult = null;
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2015-10-14 00:36:43 +08:00
|
|
|
catch (e) {}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
|
|
|
return mResult;
|
|
|
|
};
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2015-01-27 05:06:00 +08:00
|
|
|
module.exports = LocalStorageDriver;
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
}());
|