Created
July 7, 2014 22:15
-
-
Save stefbowerman/11870e4c253f70ceca16 to your computer and use it in GitHub Desktop.
Simple shopping cart class written in coffeescript
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
| # Items have the following properties : ID (string), size (string), sizeDisplay (string), price (int) | |
| class ShoppingCart | |
| constructor: (@shipping = 5) -> | |
| @items = [] | |
| _listItems: -> @items | |
| addItem: (item) -> | |
| @items.push item | |
| $(window).trigger 'shopping-cart:item-added', [item] | |
| removeItem: (index) -> | |
| @items.splice index, 1 | |
| $(window).trigger 'shopping-cart:item-removed', [index] | |
| calculateTotal: -> | |
| total = 0 | |
| for i in [0...@items.length] by 1 | |
| total += @items[i].price | |
| total + @shipping; | |
| getTotal: -> do @calculateTotal | |
| numItems: -> @items.length | |
| getItem: (i) -> @items[i] | |
| getItems: -> @items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment