The Full Controller

Listing 28.29 shows the full myApp code, with the shoppingController initialization and all the controller code together so you can see how everything fits together. Notice that the shoppingController definition includes dependencies on $scope, $http, and $window. The $window dependency allows you to add the browser alert message when errors occur.

Listing 28.29 cart_app.js-full: Implementing an application that supports keeping track of items in a shopping cart and handling the checkout process


001 var app = angular.module('myApp', []);
002 app.controller('shoppingController', ['$scope', '$http', '$window',
003                               function($scope, $http, $window) {
004     $scope.months = [1,2,3,4,5,6,7,8,9,10,11,12];
005     $scope.years = [2014,2015,2016,2017,2018,2019,2020];
006     $scope.content = '/static/products.html';
007     $http.get('/products/get')
008      .success(function(data, status, headers, config) {
009         $scope.products = data;
010         $scope.product = data[0];
011       })
012       .error(function(data, status, headers, config) {
013         $scope.products = [];
014       });
015     $http.get('/customers/get')
016      .success(function(data, status, headers, config) {
017        $scope.customer = data;
018       })
019      .error(function(data, status, headers, config) {
020        $scope.customer = [];
021      });
022     $http.get('/orders/get')
023     .success(function(data, status, headers, config) {
024        $scope.orders = data;
025      })
026      .error(function(data, status, headers, config) {
027        $scope.orders = [];
028      });
029     $scope.setContent = function(filename){
030       $scope.content = '/static/'+ filename;
031     };
032     $scope.setProduct = function(productId){
033       $scope.product = this.product;
034       $scope.content = '/static/product.html';
035     };
036     $scope.cartTotal = function(){
037       var total = 0;
038       for(var i=0; i<$scope.customer.cart.length; i++){
039         var item = $scope.customer.cart[i];
040         total += item.quantity * item.product[0].price;
041       }
042       $scope.shipping = total*.05;
043       return total+$scope.shipping;
044     };
045     $scope.addToCart = function(productId){
046       var found = false;
047       for(var i=0; i<$scope.customer.cart.length; i++){
048         var item = $scope.customer.cart[i];
049         if (item.product[0]._id== productId){
050           item.quantity += 1;
051           found = true;
052         }
053       }
054       if (!found){
055         $scope.customer.cart.push({quantity: 1,
056                                    product: [this.product]});
057       }
058       $http.post('/customers/update/cart',
059                  { updatedCart: $scope.customer.cart })
060        .success(function(data, status, headers, config) {
061          $scope.content = '/static/cart.html';
062        })
063        .error(function(data, status, headers, config) {
064          $window.alert(data);
065        });
066     };
067     $scope.deleteFromCart = function(productId){
068       for(var i=0; i<$scope.customer.cart.length; i++){
069         var item = $scope.customer.cart[i];
070         if (item.product[0]._id== productId){
071           $scope.customer.cart.splice(i,1);
072           break;
073         }
074       }
075       $http.post('/customers/update/cart',
076                  { updatedCart: $scope.customer.cart })
077        .success(function(data, status, headers, config) {
078          $scope.content = '/static/cart.html';
079        })
080        .error(function(data, status, headers, config) {
081          $window.alert(data);
082        });
083     };
084     $scope.checkout = function(){
085       $http.post('/customers/update/cart',
086                  { updatedCart: $scope.customer.cart })
087        .success(function(data, status, headers, config) {
088          $scope.content = '/static/shipping.html';
089        })
090        .error(function(data, status, headers, config) {
091          $window.alert(data);
092        });
093     };
094     $scope.setShipping = function(){
095       $http.post('/customers/update/shipping',
096           { updatedShipping :$scope.customer.shipping[0] })
097         .success(function(data, status, headers, config) {
098           $scope.content = '/static/billing.html';
099         })
100         .error(function(data, status, headers, config) {
101           $window.alert(data);
102         });
103     };
104     $scope.verifyBilling = function(ccv){
105       $scope.ccv = ccv;
106       $http.post('/customers/update/billing',
107           { updatedBilling: $scope.customer.billing[0], ccv: ccv})
108         .success(function(data, status, headers, config) {
109           $scope.content = '/static/review.html';
110         })
111         .error(function(data, status, headers, config) {
112           $window.alert(data);
113         });
114     };
115     $scope.makePurchase = function(){
116       $http.post('/orders/add',
117           { orderBilling: $scope.customer.billing[0],
118             orderShipping: $scope.customer.shipping[0],
119             orderItems: $scope.customer.cart })
120         .success(function(data, status, headers, config) {
121           $scope.customer.cart = [];
122           $http.get('/orders/get')
123           .success(function(data, status, headers, config) {
124              $scope.orders = data;
125              $scope.content = '/static/orders.html';
126            })
127            .error(function(data, status, headers, config) {
128              $scope.orders = [];
129            });
130         })
131         .error(function(data, status, headers, config) {
132           $window.alert(data);
133         });
134     };
135   }]);


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.205.165