Skip to content

Instantly share code, notes, and snippets.

@got5
Last active August 29, 2015 14:11
Show Gist options
  • Save got5/b1ea055510464e3a5dc2 to your computer and use it in GitHub Desktop.
Save got5/b1ea055510464e3a5dc2 to your computer and use it in GitHub Desktop.
Slide 110 directives_use_tp TP7
$scope.getRatingClass = ProductUtils.getRatingCss;
$scope.addToCart = function(pItem){
UserService.addToCart(pItem,1);
$location.path('/basket');
};
<article class="k-product hproduct" ng-repeat="product in products">
<div class="photo-data">
<p>
<a ng-href="#/book/{{product.id}}">
<img class="photo" ng-src="img/catalog/{{product.id}}.jpg" />
</a>
</p>
</div>
<p class="extra new" ng-if="product.isNew">new</p>
<h3 class="fn name">
<a ng-href="#/book/{{product.id}}" title="See Product sheet">{{product.name}}</a>
</h3>
<p ng-class="getRatingClass(product)">
<span ng-show="product.comments">
<a href="#comments">{{product.comments.length}} comment(s)</a>
</span>
<span ng-hide="product.comments">
No comment
</span>
<div class="price-data">
<p class="all-prices">
<strong class="price new">{{product.price | currency}}</strong>
</p>
</div>
<div class="buyZone">
<p class="buy">
<a href="#/basket" ng-click="addToCart(product)">Add to basket</a>
</p>
</div>
.controller('BasketController', ['$scope', 'UserService', 'User' , function ($scope, UserService) {
$scope.items = UserService.getCurrentUser().cart.getItems();
$scope.nbItems = $scope.items.length;
$scope.getTotal = function () {
var UserService = function ($http, $q, $log, $cookies, $cookieStore, User, isDebugMode) {
/** Currently logged user. */
var currentUser = null;
var saveCurrentUser = function(){
$cookieStore.put('cart',currentUser.cart.getItems());
$cookieStore.put('user',currentUser);
};
this.getCurrentUser = function () {
if (!currentUser) {
currentUser = $cookieStore.get('user');
if (!currentUser) {
currentUser = User.createUser();
saveCurrentUser();
}else{
currentUser.cart = User.createCart($cookieStore.get('cart'));
}
}
return currentUser;
};
/** Add an item with a given quantity in the user basket. */
this.addToCart = function (pItem, pQty) {
this.getCurrentUser().cart.setItemQty(pItem, pQty);
saveCurrentUser();
};
.controller('CatalogController', ['$scope', '$location', 'catalogService','ProductUtils', 'UserService', function ($scope, $location, catalogService, ProductUtils, UserService) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment