Making the Purchase

You call the makePurchase() function, shown in Listing 28.28, when a user clicks the Make Purchase button in the billing view. This method sends an $http POST method to the /orders/add route on the server. The POST request contains orderBilling, orderShipping, and orderItems parameters. If the request is successful, the $scope.customer.cart is initialized to [] to match the empty array value that will be set in the customer document in MongoDB.

Also, if the request is successful, a new order document will have been created in the MongoDB database. Therefore, you make another $http request, this time to /orders/get, to get the full list of orders, including the new one. Then the view switches to orders.html.

Listing 28.28 cart_app.js-makePurchase: Implementing the purchase function in the controller


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     };


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

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