@@ -3,13 +3,17 @@ import 'package:di/di.dart';
33import 'rating_component.dart' ;
44import 'recipe.dart' ;
55
6- @NgFilter (name: 'searchfilter' )
7- class SearchFilter {
8- call (value, filterString) {
9- if (value is List && filterString != null ) {
10- return value.where ((i) => i.name.contains (filterString)).toList ();
6+ @NgFilter (name: 'categoryfilter' )
7+ class CategoryFilter {
8+ call (recipeList, filterMap) {
9+ if (recipeList is List && filterMap != null && filterMap is Map ) {
10+ // If there is nothing checked, treat it as everything is checked.
11+ bool nothingChecked = filterMap.values.every ((isChecked) => ! isChecked);
12+ if (nothingChecked) {
13+ return recipeList.toList ();
14+ }
15+ return recipeList.where ((i) => filterMap[i.category] == true ).toList ();
1116 }
12- return value.toList ();
1317 }
1418}
1519
@@ -18,12 +22,22 @@ class SearchFilter {
1822 publishAs: 'ctrl' )
1923class RecipeBookController {
2024
25+ static const String LOADING_MESSAGE = "Loading recipe book..." ;
26+ static const String ERROR_MESSAGE = """Sorry! The cook stepped out of the
27+ kitchen and took the recipe book with him!""" ;
28+
2129 Http _http;
2230
23- List categories = ["Appetizers" , "Salads" , "Soups" , "Main Dishes" ,
24- "Side Dishes" , "Desserts" ];
31+ String loadingMessage = "Loading recipe book..." ;
32+
33+ // Data objects that are loaded from the server side via json
34+ List categories = [];
2535 List <Recipe > recipes = [];
2636
37+ // Filter box
38+ Map <String , bool > categoryFilterMap = {};
39+ String nameFilter = "" ;
40+
2741 RecipeBookController (Http this ._http) {
2842 _loadData ();
2943 }
@@ -34,21 +48,40 @@ class RecipeBookController {
3448 selectedRecipe = recipe;
3549 }
3650
51+ void clearFilters () {
52+ categoryFilterMap.keys.forEach ((f) => categoryFilterMap[f] = false );
53+ nameFilter = "" ;
54+ }
55+
3756 void _loadData () {
3857 _http.get ('/angular.dart.tutorial/Chapter_04/recipes.json' )
3958 .then ((HttpResponse response) {
4059 for (Map recipe in response.data) {
41- recipes.add (new Recipe .fromJson (recipe));
60+ recipes.add (new Recipe .fromJsonMap (recipe));
4261 }
62+ },
63+ onError: (Object obj) {
64+ loadingMessage = ERROR_MESSAGE ;
4365 });
66+
67+ _http.get ('/angular.dart.tutorial/Chapter_04/categories.json' )
68+ .then ((HttpResponse response) {
69+ for (String category in response.data) {
70+ categories.add (category);
71+ categoryFilterMap[category] = false ;
72+ }
73+ },
74+ onError: (Object obj) {
75+ loadingMessage = ERROR_MESSAGE ;
76+ });
4477 }
4578}
4679
4780main () {
4881 var module = new AngularModule ()
4982 ..type (RecipeBookController )
5083 ..type (RatingComponent )
51- ..type (SearchFilter );
84+ ..type (CategoryFilter );
5285
53- bootstrapAngular ([ module] );
86+ ngBootstrap ( module: module );
5487}
0 commit comments