Hi everyone !!!
I’m working on a backbone project at the moment . I have an collection of data and i wanted to filter through them. Using a bit of backbone code I inject this function to my application. This wasn’t a pain at all. However after a bit of playing with AngularJS, I thought it would be cool to show you how filtering works both worlds!!
BackboneJS:
First I add this method to my collection :
1 2 3 4 5 6 7 8 9 10 11 12 |
|
using underscore filter method that baked into backbone collections I filter through this collection by using a simple regular expression and testing it with all models name!
Now I have an searchItemView that I can use it’s module everywhere in my application that i want to search though collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
I just pass the collection and parent view to it and it will filter my collection and re-render each model. It will empty the view and using addOne re-render filtered result. addOne method is a method in my parentView that render each item into collection view.
That’s it !!
AngularJS:
Now i’m going to show you a little magic of angular and it’s directives !! I still love my backbone, but it’s so cool :D !!!!
It’s all the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
All these html are there just to show you whats happening there but the magic is when we bind filter directive to search model<li ng-repeat='name in names | filter:search'></li>
.
Thats it !!!