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

.<br />

This was extracted from app/views/index.blade.php.<br />

We’ve added three new concepts here:<br />

1. We’re filtering the ng-repeat directive with a call to products.filterByCategory(). We’ll<br />

create this in a moment, but it’s important to understand that filter allows functions to define<br />

how the items being looped are filtered.<br />

2. We’ve added ng-click directives. These directives allow the execution of logic when the<br />

element is clicked. We’re targeting another method we’re about to create; which will set the<br />

current category filter.<br />

3. We’ve added ng-class directives. These will set the defined class based on controller/scope<br />

logic. If the set category filter matches that which the button is being created from; the active<br />

class will be applied to the button.<br />

These directives, in isolation, will only cause errors. We need to add the JavaScript logic to back<br />

them up:<br />

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

2 $scope,<br />

3 CategoryService,<br />

4 ProductService<br />

5 ) {<br />

6<br />

7 var self = this;<br />

8<br />

9 // ...<br />

10<br />

11 this.category = null;<br />

12<br />

13 this.filterByCategory = function(product) {<br />

14<br />

15 if (self.category !== null) {<br />

16 return product.category.id === self.category.id;<br />

17 }<br />

18<br />

19 return true;<br />

20<br />

21 };<br />

22

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

Saved successfully!

Ooh no, something went wrong!