03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

E-Commerce 267<br />

43<br />

44 items.push({<br />

45 "product_id" : product.id,<br />

46 "quantity" : product.quantity<br />

47 });<br />

48<br />

49 }<br />

50<br />

51 return $http.post("/order/add", {<br />

52 "account" : account.id,<br />

53 "items" : JSON.stringify(items),<br />

54 "number" : number,<br />

55 "expiry" : expiry,<br />

56 "security" : security<br />

57 });<br />

58 }<br />

59 };<br />

60 });<br />

.<br />

This was extracted from public/js/shared.js.<br />

The AccountService object has a method for authenticating (with a provided email and password)<br />

and it returns the result of a POST request to /account/authenticate. It also has a getAccount()<br />

method which just returns whatever’s in the account variable.<br />

The OrderService object as a single method for sending order details to the server. I’ve bundled the<br />

payment particulars in with this method to save some time. The idea is that the order is created and<br />

paid for in a single process.<br />

We need to amend the basket controller:<br />

1 app.controller("basket", function(<br />

2 $scope,<br />

3 AccountService,<br />

4 BasketService,<br />

5 OrderService<br />

6 ) {<br />

7<br />

8 // ...<br />

9<br />

10 this.state = "shopping";

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!