Fix local storage (Closes #303)

This commit is contained in:
RainLoop Team 2014-09-02 15:34:17 +04:00
parent 77870442fe
commit 664c1dca80
11 changed files with 147 additions and 103 deletions

View file

@ -11,8 +11,8 @@
{
var
NextStorageDriver = require('_').find([
require('Storage:LocalStorage:Cookie'),
require('Storage:LocalStorage:LocalStorage')
require('Storage:LocalStorage:LocalStorage'),
require('Storage:LocalStorage:Cookie')
], function (NextStorageDriver) {
return NextStorageDriver && NextStorageDriver.supported();
})
@ -26,6 +26,9 @@
}
}
/**
* @type {LocalStorageDriver|CookieDriver|null}
*/
LocalStorage.prototype.oDriver = null;
/**

View file

@ -17,37 +17,46 @@
*/
function CookieDriver()
{
}
/**
* @static
* @return {boolean}
*/
CookieDriver.supported = function ()
{
return true;
return !!(window.navigator && window.navigator.cookieEnabled);
};
/**
* @param {string} sKey
* @param {*} mData
* @returns {boolean}
* @return {boolean}
*/
CookieDriver.prototype.set = function (sKey, mData)
{
var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName),
mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
bResult = false,
mResult = null
;
try
{
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue);
if (!mResult)
{
mResult = {};
}
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
}
catch (oException) {}
mResult[sKey] = mData;
$.cookie(Consts.Values.ClientSideCookieIndexName, JSON.stringify(mResult), {
if (!mResult)
{
mResult = {};
}
mResult[sKey] = mData;
try
{
$.cookie(Consts.Values.ClientSideStorageIndexName, JSON.stringify(mResult), {
'expires': 30
});
@ -60,18 +69,18 @@
/**
* @param {string} sKey
* @returns {*}
* @return {*}
*/
CookieDriver.prototype.get = function (sKey)
{
var
mCokieValue = $.cookie(Consts.Values.ClientSideCookieIndexName),
mStorageValue = $.cookie(Consts.Values.ClientSideStorageIndexName),
mResult = null
;
try
{
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue);
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey]))
{
mResult = mResult[sKey];

View file

@ -19,6 +19,10 @@
{
}
/**
* @static
* @return {boolean}
*/
LocalStorageDriver.supported = function ()
{
return !!window.localStorage;
@ -27,26 +31,32 @@
/**
* @param {string} sKey
* @param {*} mData
* @returns {boolean}
* @return {boolean}
*/
LocalStorageDriver.prototype.set = function (sKey, mData)
{
var
mCookieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null,
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
bResult = false,
mResult = null
;
try
{
mResult = null === mCookieValue ? null : JSON.parse(mCookieValue);
if (!mResult)
{
mResult = {};
}
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
}
catch (oException) {}
mResult[sKey] = mData;
window.localStorage[Consts.Values.ClientSideCookieIndexName] = JSON.stringify(mResult);
if (!mResult)
{
mResult = {};
}
mResult[sKey] = mData;
try
{
window.localStorage[Consts.Values.ClientSideStorageIndexName] = JSON.stringify(mResult);
bResult = true;
}
@ -57,18 +67,18 @@
/**
* @param {string} sKey
* @returns {*}
* @return {*}
*/
LocalStorageDriver.prototype.get = function (sKey)
{
var
mCokieValue = window.localStorage[Consts.Values.ClientSideCookieIndexName] || null,
mStorageValue = window.localStorage[Consts.Values.ClientSideStorageIndexName] || null,
mResult = null
;
try
{
mResult = null === mCokieValue ? null : JSON.parse(mCokieValue);
mResult = null === mStorageValue ? null : JSON.parse(mStorageValue);
if (mResult && !Utils.isUnd(mResult[sKey]))
{
mResult = mResult[sKey];