Skip to content

Instantly share code, notes, and snippets.

@darrylhebbes
Last active August 29, 2015 14:01
Show Gist options
  • Save darrylhebbes/68685624e2fd5f17e293 to your computer and use it in GitHub Desktop.
Save darrylhebbes/68685624e2fd5f17e293 to your computer and use it in GitHub Desktop.
AngularJS Cookie factory based on quirksmode article http://www.quirksmode.org/js/cookies.html
'use strict';
angular.module('CookieCompany', [])
.factory('Bakery', function ($document) {
return {
create : function(name, ingredients, duration) {
if (duration) {
var date = new Date();
date.setTime(date.getTime()+(duration*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
$document.cookie = name+"="+ingredients+expires+"; path=/";
},
read : function(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;
},
trash : function(name) {
this.set(name,"",-1);
}
}
});
@darrylhebbes
Copy link
Author

This is by no means a replacement for Angular's own $cookies https://docs.angularjs.org/api/ngCookies/service/$cookies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment