03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

E-Commerce 258<br />

There are some awesome things happening in this code. Firstly, we abstract the logic by which we get<br />

categories and products (from the API) into AngularJS’s implementation of services. We also have<br />

access to the $http interface; which is a wrapper for XMLHTTPRequest, and acts as a replacement<br />

for other libraries (think jQuery) which we would have used before.<br />

The two services each have a method for returning the API data, for categories and products. These<br />

methods return things, called promises, which are references to future-completed data. We attach<br />

callbacks to these, within the ProductController, which essentially update the controller data.<br />

So we have the API data, but how do we render it in the interface? We do so with directives and<br />

data-binding:<br />

1 <br />

2 <br />

3 Products<br />

4 <br />

5 <br />

6 <br />

10 All<br />

11 <br />

12 <br />

17 @{{ category.name }}<br />

18 <br />

19 <br />

20 <br />

21 <br />

25 <br />

29 Add to basket<br />

30 <br />

31 <br />

32 @{{ product.name }}

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

Saved successfully!

Ooh no, something went wrong!