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 263<br />

.<br />

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

We’re grouping all the basket-related logic together in BasketService. You may have noticed the<br />

reference to ngCookies (when creating the app module) and the extra script file reference (in<br />

index.blade.php). These allow us to use AngularJS’s cookies module; for storing the basket items.<br />

The getProducts() method returns the products. We need to store them as a serialised JSON array,<br />

so when we initially retrieve them; we parse them (with a default value of ”[]”). The add() and<br />

remove() methods create and destroy special item objects. After each basket item operation; we<br />

need to persist the products array back to $cookies.<br />

The update() method works out the total cost of each item; by taking into account the original price<br />

and the updated quantity. It also rounds this value to avoid floating-point calculation irregularities.<br />

There’s also a store() method which persists the products to $cookies, and a clear() method which<br />

removes all products.<br />

The HTML, compatible with all this, is:<br />

1 <br />

2 <br />

3 Basket<br />

4 <br />

5 <br />

6 <br />

7 <br />

11 <br />

12 @{{ product.name }}<br />

13 <br />

14 <br />

15 <br />

22 <br />

23

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

Saved successfully!

Ooh no, something went wrong!