Last active
August 29, 2015 14:02
-
-
Save aaronroberson/719ec9627bb75f8ac3f8 to your computer and use it in GitHub Desktop.
Revisions
-
aaronroberson revised this gist
Jun 20, 2014 . 1 changed file with 35 additions and 23 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,21 +12,29 @@ var cart = { getItems: function() { // Initialize the itemsCookie variable var itemsCookie; // Check if cart is empty if(!items.length) { // Get the items cookie itemsCookie = $cookieStore.get('items'); // Check if the item cookie exists if(itemsCookie) { // Loop through the items in the cookie angular.forEach(itemsCookie, function(item, key) { // Get the product details from the ProductService using the guid ProductService.getProduct(key).then(function(response){ var product = response.data; // Update the quantity to the quantity saved in the cookie product.quantity = item; // Add the product to the cart items object using the guid as the key items[product.guid] = product; }); }); } } // Returns items object return items; }, addItem: function(item) { @@ -61,6 +69,7 @@ emptyCart: function() { // Sets items array to empty array items = {}; // Remove the items cookie $cookieStore.remove('items'); }, @@ -91,15 +100,18 @@ return cart.getCartSubtotal(); }, updateItemsCookie: function() { // Initialize an object to be saved as the cookie var itemsCookie = {}; // Loop through the items in the cart angular.forEach(items, function(item, key) { // Add each item to the items cookie, // using the guid as the key and the quantity as the value itemsCookie[key] = item.quantity; }); // Use the $cookieStore service to persist the itemsCookie object to the cookie named 'items' $cookieStore.put('items', itemsCookie); }, getItemPrice: function(item) { return parseFloat(item.isSpecial ? item.specialPrice : item.price); -
aaronroberson revised this gist
Jun 20, 2014 . 2 changed files with 8 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ getCartTotal: function() { // TODO Return the cartSubtotal plus shipping and handling // TODO Return the item quantity times item price for each item in the array return cart.getCartSubtotal(); }, 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ getItemPrice: function(item) { return parseFloat(item.isSpecial ? item.specialPrice : item.price); } -
aaronroberson created this gist
Jun 20, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,113 @@ (function(angular) { "use strict"; var app = angular.module('MyStore'); app.factory('CartService', function($cookieStore, ProductService) { // Private items variable var items = {}; // Angular factories return service objects var cart = { getItems: function() { var itemsCookie; // Returns items object if(!items.length) { itemsCookie = $cookieStore.get('items'); if(itemsCookie) { angular.forEach(itemsCookie, function(item, key) { ProductService.getProduct(key).then(function(response){ var product = response.data; product.quantity = item; items[product.guid] = product; }); }); } } return items; }, addItem: function(item) { // Checks if item already exists // If it exists, updates the quantity // If it doesn't exist, adds quantity property with value of 1 then // pushes the item onto the items array if(!items[item.guid]) { item.quantity = 1; items[item.guid] = item; } else { items[item.guid].quantity += 1; //items[item.guid].quantity = items[item.guid].quantity + 1 } cart.updateItemsCookie(); }, removeItem: function(guid) { // Removes an item from the items array // Can use angular.forEach(array, function(item, index) to splice delete items[guid]; // var myArr = new Array(); // delete myArr.splice // returns false // delete myArr.length // return false // var x = 10; // delete x; // return false // y = 10; // delete y; // return true cart.updateItemsCookie(); }, emptyCart: function() { // Sets items array to empty array items = {}; $cookieStore.remove('items'); }, getItemCount: function() { // returns number of items, including item quantity var total = 0; angular.forEach(items, function(item) { total += item.quantity; }); return total; }, getCartSubtotal: function() { // Return the item quantity times item price for each item in the array var total = 0; angular.forEach(items, function(item) { var s = parseInt(item.quantity); var q = isNaN(s) ? 0 : s; var p = cart.getItemPrice(item); total += q * p; }); return total; }, getCartTotal: function() { // TODO Return the cartSubtotal plus shipping and handling // TODO Return the item quantity times item price for each item in the array return cart.getCartSubtotal(); }, // aaronaroberson@gmail.com updateItemsCookie: function() { var itemsCookie = {}; angular.forEach(items, function(item, key) { itemsCookie[key] = item.quantity; }); $cookieStore.put('items', itemsCookie); }, getItemPrice: function(item) { return parseFloat(item.isSpecial ? item.specialPrice : item.price); } }; return cart; }); })(window.angular); 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ emptyCart: function() { // Sets items array to empty array items = {}; // Remove the items cookie $cookieStore.remove('items'); }, 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ getItems: function() { // Initialize the itemsCookie variable var itemsCookie; // Check if cart is empty if(!items.length) { // Get the items cookie itemsCookie = $cookieStore.get('items'); // Check if the item cookie exists if(itemsCookie) { // Loop through the items in the cookie angular.forEach(itemsCookie, function(item, key) { // Get the product details from the ProductService using the guid ProductService.getProduct(key).then(function(response){ var product = response.data; // Update the quantity to the quantity saved in the cookie product.quantity = item; // Add the product to the cart items object using the guid as the key items[product.guid] = product; }); }); } } // Returns items object return items; } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ updateItemsCookie: function() { // Initialize an object to be saved as the cookie var itemsCookie = {}; // Loop through the items in the cart angular.forEach(items, function(item, key) { // Add each item to the items cookie, // using the guid as the key and the quantity as the value itemsCookie[key] = item.quantity; }); // Use the $cookieStore service to persist the itemsCookie object to the cookie named 'items' $cookieStore.put('items', itemsCookie); },