ƒ.storage - Get-Kraken/fixx GitHub Wiki

Feature test

fixx can test for sessionStorage, localStorage and cookie

ƒ.storage.caniuse.sessionStorage(); // → true/false
ƒ.storage.caniuse.localStorage(); // → true/false
ƒ.storage.caniuse.cookie(); // → true/false

ƒ.storage.cookie

ƒ provides a cookie framework to handle the scoping, reading/writing and data typing of cookies. Cookies need to be defined before they are instantiated: for example, in a config section:

myApp.config = {
cookies: [
      { name: 'testCookie', expire: 365, type: 'string' }
    , { name: 'joinDate', expire: 7, type: 'date' }
    ]
};

name: name for the cookie
expire: int in days between 0 and 365 (0 will cause the cookie to be a session cookie)
type: boolean, date, string, integer, float, object

Cookie scope:

cookies are scoped with 'fixx_' by default. When referring to a cookie, only the original given name is required.

This can be changed to e.g. 'myApp_' before initialising the cookies

ƒ.storage.cookie.namespace = 'myApp_';

After defining the cookies, initialise them on $(document).ready():

ƒ.storage.cookie.init(myApp.config.cookies);

cookies can now be accessed as follows:

ƒ.cookies.testCookie or ƒ.cookies['testCookie'];

Methods and properties available on a cookie instance:

cookieName given name in cookie instance
data null for new cookies / restored data on existing cookies
dataType return type given in cookie instance
expireTime int number of days as set in cookie instance
destroy() delete cookie and remove from DOM
isSet() false for new cookie instances, true for existing cookies.
update(data) updates .data property and writes serialised data to cookie

Cookie examples:

cookie declared and page loaded for the first time

  • get cookie name
ƒ.cookies.testCookie.cookieName; // → 'testCookie'
  • check if a cookie is set
    if the cookie doesn't exist (on first load) it will return false.
    else if the cookie exists, it will return true
ƒ.cookies.testCookie.isSet(); // → false
  • get cookie data
    null for new instances that haven't been updated with data yet.
ƒ.cookies.testCookie.data; // → null
  • get the cookie's data type
ƒ.cookies.testCookie.dataType; // → 'string'
  • update a cookie's data value
    (should be of dataType that the cookie was created with)
ƒ.cookies.testCookie.update('foobar'); // → true
  • read back cookie's data
ƒ.cookies.testCookie.data; // → 'foobar'
  • on page reload the isSet() response will be true
ƒ.cookies.testCookie.isSet(); // → true
  • and the data will return 'foobar'
ƒ.cookies.testCookie.data; // → 'foobar'