Skip to content

Instantly share code, notes, and snippets.

@aaronroberson
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save aaronroberson/0f8e21ee45eb11e5d589 to your computer and use it in GitHub Desktop.

Select an option

Save aaronroberson/0f8e21ee45eb11e5d589 to your computer and use it in GitHub Desktop.

Revisions

  1. aaronroberson revised this gist Jul 16, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion updateItemsCookie
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    updateItemsCookie: function() {
    function updateItemsCookie() {
    // Initialize an object to be saved as the cookie
    var itemsCookie = {};
    // Loop through the items in the cart
  2. aaronroberson created this gist Jul 16, 2014.
    6 changes: 6 additions & 0 deletions emptyCart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    emptyCart: function() {
    // Sets items object to an empty object
    items = {};
    // Remove the items cookie
    $cookieStore.remove('items');
    }
    25 changes: 25 additions & 0 deletions getItems
    Original 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
    SwagService.get({id: 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;
    }
    12 changes: 12 additions & 0 deletions updateItemsCookie
    Original 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);
    }