Skip to content

Instantly share code, notes, and snippets.

@pawelgur
Last active August 29, 2015 14:23
Show Gist options
  • Save pawelgur/24e021f2714dbb6b076a to your computer and use it in GitHub Desktop.
Save pawelgur/24e021f2714dbb6b076a to your computer and use it in GitHub Desktop.
Helper for working with cookies
var CookieHelper = {
namespace: "",
////////////////// BASE METHODS /////////////////////
createCookie : function (name, value, days) {
var expires;
name = this.namespace + "." + name;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
},
readCookie : function (name) {
name = this.namespace + "." + name;
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0)
{
return c.substring(nameEQ.length, c.length);
}
}
return null;
},
eraseCookie : function eraseCookie(name) {
name = this.namespace + "." + name;
this.createCookie(name, "", -1);
},
areCookiesEnabled : function () {
var r = false;
this.createCookie("testing", "Hello", 1);
if (this.readCookie("testing") !== null) {
r = true;
this.eraseCookie("testing");
}
return r;
},
////////////////// BACKBONE MODELS /////////////////////
//reads backbone model properties if blank
readModel : function(model, fields)
{
for (var i = 0; i < fields.length; i++)
{
var fieldName = fields[i];
if (!model.get(fieldName))
{
var value = this.readCookie(fieldName);
model.set(fieldName, value);
}
}
},
writeModel : function(model, fields)
{
for (var i = 0; i < fields.length; i++)
{
var fieldName = fields[i];
var value = model.get(fieldName);
if (value || value === false || value === "")
{
this.createCookie(fieldName, value, 30);
}
}
},
////////////////////// CSV ARRAYS //////////////////////////
//adds value to cookie array if it is not there
addToArray : function(value, key, days)
{
var array = this.getArray(key);
if (array.indexOf(value) === -1)
{
array.push(value);
this.setArray(array, key, days);
}
},
//removes value from array
removeFromArray : function(value, key)
{
var array = this.getArray(key);
var index = array.indexOf(value);
if (index >= 0)
{
array.splice(index, 1);
this.setArray(array, key);
}
},
//gets array from cookie (or creates new one)
getArray : function(key)
{
var cookieValue = this.readCookie(key);
var array = [];
if (cookieValue)
{
array = JSON.parse(cookieValue);
}
return array;
},
//sets array as cookie
setArray : function(array, key, days)
{
days = typeof days === 'undefined' ? 1000 : days;
var cookieValue = JSON.stringify(array);
this.createCookie(key, cookieValue, days);
},
//////////////// test ///////////////////
test : function()
{
var cookieKey = "CookieHelperTest";
console.log("cookies enabled: " + CookieHelper.areCookiesEnabled());
CookieHelper.createCookie(cookieKey, "", 1000);
//array tests
CookieHelper.addToArray(1, cookieKey);
CookieHelper.addToArray(2, cookieKey);
console.log("expected: 1, 2");
console.log("real: " + CookieHelper.readCookie(cookieKey));
CookieHelper.removeFromArray(1, cookieKey);
console.log("expected: 2");
console.log("real: " + CookieHelper.readCookie(cookieKey));
var array = [3, 4, 5];
CookieHelper.setArray(array, cookieKey);
console.log("expected: 3, 4, 5");
console.log("real: " + CookieHelper.readCookie(cookieKey));
CookieHelper.removeFromArray(3, cookieKey);
CookieHelper.removeFromArray(5, cookieKey);
console.log("expected: 4");
console.log("real: " + CookieHelper.readCookie(cookieKey));
var test = CookieHelper.getArray(cookieKey);
console.log("type: " + typeof test + ", array: " + test);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment