Created
April 6, 2009 02:50
-
-
Save robertsosinski/90619 to your computer and use it in GitHub Desktop.
Handle cookies betwen JavaScript and Rails with JSON/Hash objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.cookies = { | |
set: function(name, value, days) { | |
if (name) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
var expires = "; expires=" + date.toGMTString(); | |
} | |
else { | |
var expires = ""; | |
} | |
document.cookie = name + "=" + escape(value) + expires + "; path=/"; | |
} | |
}, | |
setJSON: function(name, value, days) { | |
this.set(name, Object.toJSON(value), days); // toJSON() requires Prototype.js | |
}, | |
get: function(name) { | |
if (name) { | |
var match = document.cookie.match(new RegExp(name + "=(.*?)(?:;|$)")); | |
if (match) { | |
return unescape(match[1].replace(/\+/g, "%20")); | |
} | |
} | |
}, | |
getJSON: function(name) { | |
var value = this.get(name); | |
if (value) { | |
return value.evalJSON(); // evalJSON() requires Prototype.js | |
} | |
}, | |
list: function() { | |
var matches = document.cookie.split(new RegExp("=|; ")); | |
if (matches.length > 1) { | |
var cookies = new Array(matches.length / 2); | |
for (var i = j = 0; j < matches.length; j+=2) { | |
cookies[i++] = matches[j] | |
} | |
return cookies; | |
} | |
}, | |
delete_: function(name) { | |
this.set(name, "" , -1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_filter :json_cookies | |
protected | |
def json_cookies | |
request.cookies.each do |key, value| | |
request.cookies[key] = ActiveSupport::JSON.decode(request.cookies[key]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// client side | |
// using prototype.js | |
cookies.setJSON('data', [{"name": "alice", "job": "programmer"}, {"name": "bob", "job": "administrator"}]); | |
cookies.getJSON('data') // [Object name=alice job=programmer, Object name=bob job=administrator] | |
cookies.getJSON('data').first() // Object name=alice job=programmer | |
cookies.getJSON('data').first().name; // "alice" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# server side | |
cookies["data"] # => [{"name" => "alice", "job" => "programmer"}, {"name" => "bob", "job" => "administrator"}] | |
cookies["data"].first # => {"name" => "alice", "job" => "programmer"} | |
cookies["data"].first["name"] # => "alice" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment