From 50460daa9e18fb3620692127ceabc87f4e1c5dac Mon Sep 17 00:00:00 2001 From: Aaron Quint Date: Fri, 1 Jan 2010 17:44:38 -0500 Subject: [PATCH] Store the cart in session() --- index.html | 1 + javascripts/json_store.js | 9 ++++++--- javascripts/sammy/plugins/sammy.storage.js | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 69bb561..34ba293 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,7 @@

The JSON Store

+ diff --git a/javascripts/json_store.js b/javascripts/json_store.js index 4561e33..f41070e 100644 --- a/javascripts/json_store.js +++ b/javascripts/json_store.js @@ -33,17 +33,20 @@ this.partial('templates/item_detail.template'); }); - // initialize the cart - var cart = {}; - this.post('#/cart', function(context) { var item_id = this.params['item_id']; + // fetch the current cart + var cart = this.session('cart', function() { + return {}; + }); if (!cart[item_id]) { // this item is not yet in our cart // initialize its quantity with 0 cart[item_id] = 0; } cart[item_id] += parseInt(this.params['quantity']); + // store the cart + this.session('cart', cart); this.log("The current cart: ", cart); }); diff --git a/javascripts/sammy/plugins/sammy.storage.js b/javascripts/sammy/plugins/sammy.storage.js index 856ab45..bcfd22b 100644 --- a/javascripts/sammy/plugins/sammy.storage.js +++ b/javascripts/sammy/plugins/sammy.storage.js @@ -232,7 +232,7 @@ }; $.extend(Sammy.Store.LocalStorage.prototype, { isAvailable: function() { - return ('localStorage' in window); + return ('localStorage' in window) && window.location.protocol != 'file:'; }, exists: function(key) { return (this.get(key) != null); @@ -413,7 +413,7 @@ this.helpers({ store: function() { - return this.app.apply(this.app, arguments); + return this.app.store.apply(this.app, arguments); } }); };