Created
October 27, 2010 02:39
-
-
Save gus/648314 to your computer and use it in GitHub Desktop.
Shopping cart express+node middleware
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
var Cart = function(storedItems) { | |
var items = storedItems || {}; | |
this.add = function(item, sku) { | |
var storedItem = items[sku]; | |
if (storedItem == undefined) { | |
storedItem = items[sku] = {quantity: 0, item: item} | |
} | |
storedItem.quantity += 1; | |
}; | |
// dumb name for now. figure out forEach stuff | |
this.to_a = function() { | |
var arr = []; | |
for (sku in items) { arr.push(items[sku]); } | |
return arr; | |
}; | |
this.dump = function() { return items; }; | |
}; | |
// Exports | |
exports.dandee = { | |
cart: function(req, res, next) { | |
var cart = new Cart(req.session.cart); | |
req.cart = cart; | |
next(); | |
req.session.cart = cart.dump(); | |
return; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment