Skip to content

Instantly share code, notes, and snippets.

@aaronroberson
Last active November 13, 2015 13:41
Show Gist options
  • Save aaronroberson/a26f85400a3e8818a834 to your computer and use it in GitHub Desktop.
Save aaronroberson/a26f85400a3e8818a834 to your computer and use it in GitHub Desktop.

Revisions

  1. aaronroberson revised this gist Jul 16, 2014. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions cart-service.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,11 @@

    // Private items object
    var items = {};

    // Update cookies
    function updateItemsCookie() {

    }

    // Angular factories return service objects
    return {
    @@ -37,14 +42,14 @@
    // pushes the item onto the items array

    // Update cookie
    this.updateItemsCookie();
    updateItemsCookie();
    },

    removeItem: function(id) {
    // Removes an item from the items object

    // Update cookie
    this.updateItemsCookie();
    updateItemsCookie();
    },

    emptyCart: function() {
  2. aaronroberson revised this gist Jul 13, 2014. 2 changed files with 33 additions and 0 deletions.
    1 change: 1 addition & 0 deletions add-cart-button.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <button class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i> Add to cart</button>
    32 changes: 32 additions & 0 deletions add-cart-button.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    (function(angular) {
    "use strict";

    var app = angular.module('Swagwise');

    // Inject the CartService
    app.directive('addCartButton', function() {

    return {
    // E for Element
    // A for Attribute
    // C for Class
    restrict: 'E',
    scope: {
    // 3 types of bindings for scope properties
    // @ which is a string
    // & which is a one-way binding
    // = which is two-way binding
    },
    replace: true,
    templateUrl: 'templates/add-cart-button.html',
    link: function(scope, elem, attr) {

    scope.addItem = function(item) {
    // Pass the item into the addItem method of the CartService
    };
    }

    };
    });

    })(window.angular);
  3. aaronroberson revised this gist Jul 13, 2014. 1 changed file with 11 additions and 7 deletions.
    18 changes: 11 additions & 7 deletions cart-controller.js
    Original file line number Diff line number Diff line change
    @@ -14,23 +14,27 @@
    };

    $scope.getItemCount = function() {
    // return the item count from the CartService
    };

    $scope.removeItem = function(id) {
    // Pass the item id into the removeItem method of the CartService
    // Return the item count from the CartService
    };

    $scope.getCartSubtotal = function() {
    // return the subtotal using the getCartSubtotal method of the CartService
    // Return the subtotal using the getCartSubtotal method of the CartService
    };

    $scope.getCartTotal = function() {
    // Return the cart total using the getCartTotal methode of the CartService
    };

    $scope.removeItem = function(id) {
    // Pass the item id into the removeItem method of the CartService
    };

    $scope.emptyCart = function() {
    // Invoke the emptyCart method of the CartService
    }

    $scope.checkout = function() {
    // Call the checkout method of the CartService
    // Invoke the checkout method of the CartService
    };

    });
  4. aaronroberson created this gist Jul 13, 2014.
    38 changes: 38 additions & 0 deletions cart-controller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    (function(angular) {
    "use strict";

    var app = angular.module('Swagwise');

    // Inject in the CartService
    app.controller('CartController', function($scope) {

    // Set the items on the scope to the items in the CartService using the getItems method
    $scope.items = {};

    $scope.addItem = function(item) {
    // Pass the item into the addItem method of the CartService
    };

    $scope.getItemCount = function() {
    // return the item count from the CartService
    };

    $scope.removeItem = function(id) {
    // Pass the item id into the removeItem method of the CartService
    };

    $scope.getCartSubtotal = function() {
    // return the subtotal using the getCartSubtotal method of the CartService
    };

    $scope.getCartTotal = function() {
    // Return the cart total using the getCartTotal methode of the CartService
    };

    $scope.checkout = function() {
    // Call the checkout method of the CartService
    };

    });

    })(window.angular);
    87 changes: 87 additions & 0 deletions cart-service.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    (function(angular) {
    "use strict";

    var app = angular.module('Swagwise');

    // Inject in $cookieStore, SwagService and app config
    app.factory('CartService', function() {

    // Private items object
    var items = {};

    // Angular factories return service objects
    return {

    getItems: function() {
    // Initialize itemsCookie variable
    var itemsCookie;
    // Check if items object has been populated
    if(!items.length) {
    // Populate items object from cookie

    // Check if cookie exists
    if(itemsCookie) {
    // Loop through cookie and get the item by it's id
    // Add each item to the items object and set it's quantity

    }
    }
    // Return the items object
    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

    // Update cookie
    this.updateItemsCookie();
    },

    removeItem: function(id) {
    // Removes an item from the items object

    // Update cookie
    this.updateItemsCookie();
    },

    emptyCart: function() {
    // Re-initialize items object to an empty object
    items = {};
    // Remove items cookie using $cookieStore

    },

    getItemCount: function() {
    // Initialize total counter
    var total = 0;
    // Loop through items and increment the total by the item quantity

    // Returns number of items, including item quantity
    return total;
    },

    getCartSubtotal: function() {
    // Initialize the total counter
    var total = 0;
    // Loop through the items and multiply the quantity by the item price and increment the total

    // Return the item quantity times item price for each item in the array
    return total;
    },

    getCartTotal: function() {
    return this.getCartSubtotal();
    },

    checkout: function() {
    // Impliment the checkout
    }

    };

    });

    })(window.angular);
    30 changes: 30 additions & 0 deletions mini-cart-directive.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    (function(angular) {
    "use strict";

    var app = angular.module('Swagwise');

    // Inject in the CartService
    app.directive('miniCart', function() {

    return {
    // Create an isolated scope
    scope: {
    },
    restrict: 'E',
    replace: true,
    templateUrl: 'templates/mini-cart.html',
    link: function(scope, elem, attr) {

    scope.getCartSubtotal = function() {
    // Returns subtotal from CartService
    };

    scope.getItemCount = function() {
    //Returns the item count from the CartService
    };
    }

    };
    });

    })(window.angular);
    8 changes: 8 additions & 0 deletions mini-cart.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <!-- Use ng-show to show if the items have been added to the cart -->
    <div class="row">
    <div class="col-sm-4 col-sm-offset-8 text-right">
    <!-- Display the subtotal followed by the number of items in the cart -->
    <i class="glyphicon glyphicon-shopping-cart"></i> Subtotal: - items
    <a class="btn btn-success" ui-sref="cart" role="button">view cart</a>
    </div>
    </div>