Last active
August 29, 2015 14:02
-
-
Save aaronroberson/c666766145a71144ddef to your computer and use it in GitHub Desktop.
Geekwise Day 9 Assets
This file contains 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
(function(angular) { | |
var app = angular.module('MyStore'); | |
// Inject in the CartService | |
app.controller('CartController', function($scope, CartService) { | |
// Set the items on the scope to the items in the CartService | |
$scope.items = CartService.getItems(); | |
$scope.getItemCount = CartService.getItemCount; | |
$scope.removeItem = CartService.removeItem; | |
$scope.emptyCart = CartService.emptyCart; | |
$scope.cartSubtotal = CartService.getCartSubtotal; | |
$scope.cartTotal = CartService.getCartTotal; | |
}); | |
})(window.angular); |
This file contains 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
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore'); | |
app.factory('CartService', function() { | |
// Private items variable | |
var items = {}; | |
// Angular factories return service objects | |
return { | |
getItems: function() { | |
// Returns 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 | |
// adds the item onto the items object | |
if(!items[item.guid]) { | |
item.quantity = 1; | |
items[item.guid] = item; | |
}else { | |
items[item.guid].quantity += 1; | |
} | |
}, | |
removeItem: function(item_id) { | |
delete items[item_id]; | |
}, | |
emptyCart: function() { | |
// Sets items object to an empty object | |
items = {}; | |
}, | |
getItemCount: function() { | |
// returns number of items, including item quantity | |
var total = 0; | |
angular.forEach(items, function(item) { | |
total += parseInt(item.quantity); | |
}); | |
return total; | |
}, | |
getCartSubtotal: function() { | |
// Return the item quantity times item price for each item in the items object | |
var total = 0; | |
angular.forEach(items, function(item) { | |
total += parseInt(item.quantity) * parseFloat(item.isSpecial ? item.specialPrice : item.price); | |
}); | |
return total; | |
}, | |
getCartTotal: function() { | |
// Return the cartSubtotal plus shipping and handling | |
var total = 0; | |
angular.forEach(items, function(item) { | |
// TODO add shipping | |
total += parseInt(item.quantity) * parseFloat(item.isSpecial ? item.specialPrice : item.price); | |
}); | |
return total; | |
} | |
}; | |
}); | |
})(window.angular); |
This file contains 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
<div class="row"> | |
<h2>Your shopping cart</h2> | |
<div class="col-sm-12"> | |
<div class="alert alert-warning" ng-show="!getItemCount()"> | |
<p>There are no items in your cart | |
<a ui-sref="products" class="btn btn-default"> | |
<span class="glyphicon glyphicon-shopping-cart"></span> Continue Shopping | |
</a> | |
</p> | |
</div> | |
<table class="table table-hover" ng-show="getItemCount()"> | |
<thead> | |
<tr> | |
<th>Product</th> | |
<th>Quantity</th> | |
<th class="text-center">Price</th> | |
<th class="text-center">Total</th> | |
<th> </th> | |
</tr> | |
</thead> | |
<tbody> | |
<!-- repeat of the items in the cart --> | |
<tr ng-repeat="item in items"> | |
<td class="col-sm-8 col-md-6"> | |
<div class="media"> | |
<a class="thumbnail pull-left" ui-sref="product({guid: item.guid})"> | |
<img class="media-object" ng-src="{{item.thumbnail}}" style="width: 72px; height: 72px;"> </a> | |
<div class="media-body"> | |
<h4 class="media-heading"><a ui-shref="product({guid: item.guid})" ng-bind="item.title"></a></h4> | |
<h5 class="media-heading"> by <a href="#" ng-bind="item.manufacturer"></a></h5> | |
</div> | |
</div> | |
</td> | |
<td class="col-sm-1 col-md-1 text-center"> | |
<input type="input" class="form-control" ng-model="item.quantity"> | |
</td> | |
<td class="col-sm-1 col-md-1 text-center"><strong>{{item.isSpecial ? item.specialPrice : item.price | currency}}</strong></td> | |
<td class="col-sm-1 col-md-1 text-center"><strong>{{item.isSpecial ? item.specialPrice * item.quantity : item.price * item.quantity | currency}}</strong></td> | |
<td class="col-sm-1 col-md-1"> | |
<button type="button" class="btn btn-danger" ng-click="removeItem(item.guid)"> | |
<span class="glyphicon glyphicon-remove"></span> Remove | |
</button> | |
</td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td> </td> | |
<td> </td> | |
<td><h5>Subtotal</h5></td> | |
<td class="text-right"><h5><strong>{{cartSubtotal() | currency}}</strong></h5></td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td> </td> | |
<td> </td> | |
<td><h5>Estimated shipping</h5></td> | |
<td class="text-right"><h5><strong></strong></h5></td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td> </td> | |
<td> </td> | |
<td><h3>Total</h3></td> | |
<td class="text-right"><h3><strong>{{cartTotal() | currency}}</strong></h3></td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td> </td> | |
<td> | |
<button type="button" class="btn btn-danger" ng-click="emptyCart()"> | |
<span class="glyphicon glyphicon-remove"></span> Empty Cart | |
</button> | |
</td> | |
<td> | |
<a ui-sref="products" class="btn btn-default"> | |
<span class="glyphicon glyphicon-shopping-cart"></span> Continue Shopping | |
</a> | |
</td> | |
<td> | |
<button type="button" class="btn btn-success"> | |
Checkout <span class="glyphicon glyphicon-play"></span> | |
</button> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
</div> |
This file contains 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
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore'); | |
// Inject in the CartService | |
app.directive('msMiniCart', function(CartService) { | |
return { | |
scope: { | |
}, | |
restrict: 'E', | |
replace: true, | |
templateUrl: 'templates/mini-cart.html', | |
link: function(scope, elem, attr) { | |
scope.cartSubtotal = CartService.getCartSubtotal; | |
scope.itemCount = CartService.getItemCount; | |
} | |
}; | |
}); | |
})(window.angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment