Skip to content

Commit 3e159e4

Browse files
committed
Merge pull request dart-archive#112 from chalin/patch-0403-ch7-sync-with-ch6
Sync chap 7 code with most recent chap 6 updates
2 parents 3661444 + 5dbded9 commit 3e159e4

File tree

9 files changed

+43
-39
lines changed

9 files changed

+43
-39
lines changed

Chapter_07/lib/component/search_recipe_component.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import 'package:angular/angular.dart';
77
templateUrl: 'packages/angular_dart_demo/component/search_recipe_component.html',
88
publishAs: 'cmp')
99
class SearchRecipeComponent {
10+
Map<String, bool> _categoryFilterMap;
11+
List<String> _categories;
12+
1013
@NgTwoWay('name-filter-string')
1114
String nameFilterString = "";
1215

1316
@NgTwoWay('category-filter-map')
14-
Map<String, bool> categoryFilterMap;
17+
Map<String, bool> get categoryFilterMap => _categoryFilterMap;
18+
void set categoryFilterMap(values) {
19+
_categoryFilterMap = values;
20+
_categories = categoryFilterMap.keys.toList();
21+
}
1522

16-
List<String> get categories => categoryFilterMap.keys.toList();
23+
List<String> get categories => _categories;
1724

1825
void clearFilters() {
19-
categoryFilterMap.keys.forEach((f) => categoryFilterMap[f] = false);
26+
_categoryFilterMap.keys.forEach((f) => _categoryFilterMap[f] = false);
2027
nameFilterString = "";
2128
}
2229
}

Chapter_07/lib/filter/category_filter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CategoryFilter {
99
// If there is nothing checked, treat it as "everything is checked"
1010
bool nothingChecked = filterMap.values.every((isChecked) => !isChecked);
1111
return nothingChecked
12-
? recipeList.toList()
12+
? recipeList
1313
: recipeList.where((i) => filterMap[i.category] == true).toList();
1414
}
1515
return const [];

Chapter_07/lib/recipe_book.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import 'service/query_service.dart';
1212
class RecipeBookController {
1313

1414
static const String LOADING_MESSAGE = "Loading recipe book...";
15-
static const String ERROR_MESSAGE = """Sorry! The cook stepped out of the
16-
kitchen and took the recipe book with him!""";
15+
static const String ERROR_MESSAGE = "Sorry! The cook stepped out of the"
16+
"kitchen and took the recipe book with him!";
1717

18-
Http _http;
19-
QueryService _queryService;
20-
QueryService get queryService => _queryService;
18+
final Http _http;
19+
final QueryService queryService;
2120

2221
// Determine the initial load state of the app
2322
String message = LOADING_MESSAGE;
@@ -27,15 +26,18 @@ kitchen and took the recipe book with him!""";
2726
// Data objects that are loaded from the server side via json
2827
List<String> _categories = [];
2928
List<String> get categories => _categories;
29+
3030
Map<String, Recipe> _recipeMap = {};
3131
Map<String, Recipe> get recipeMap => _recipeMap;
32-
List<Recipe> get allRecipes => _recipeMap.values.toList();
32+
List<Recipe> _allRecipes = [];
33+
34+
List<Recipe> get allRecipes => _allRecipes;
3335

3436
// Filter box
35-
Map<String, bool> categoryFilterMap = {};
37+
final categoryFilterMap = <String, bool>{};
3638
String nameFilter = "";
3739

38-
RecipeBookController(Http this._http, QueryService this._queryService) {
40+
RecipeBookController(this._http, this.queryService) {
3941
_loadData();
4042
}
4143

@@ -58,9 +60,10 @@ kitchen and took the recipe book with him!""";
5860
}
5961

6062
void _loadData() {
61-
_queryService.getAllRecipes()
63+
queryService.getAllRecipes()
6264
.then((Map<String, Recipe> allRecipes) {
6365
_recipeMap = allRecipes;
66+
_allRecipes = _recipeMap.values.toList();
6467
recipesLoaded = true;
6568
})
6669
.catchError((e) {
@@ -69,7 +72,7 @@ kitchen and took the recipe book with him!""";
6972
message = ERROR_MESSAGE;
7073
});
7174

72-
_queryService.getAllCategories()
75+
queryService.getAllCategories()
7376
.then((List<String> allCategories) {
7477
_categories = allCategories;
7578
for (String category in _categories) {
@@ -83,4 +86,4 @@ kitchen and took the recipe book with him!""";
8386
message = ERROR_MESSAGE;
8487
});
8588
}
86-
}
89+
}

Chapter_07/lib/service/query_service.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class QueryService {
1414
Map<String, Recipe> _recipesCache;
1515
List<String> _categoriesCache;
1616

17-
Http _http;
17+
final Http _http;
1818

1919
QueryService(Http this._http) {
2020
_loaded = Future.wait([_loadRecipes(), _loadCategories()]);
@@ -32,12 +32,8 @@ class QueryService {
3232
}
3333

3434
Future _loadCategories() {
35-
return _http.get(_categoriesUrl)
36-
.then((HttpResponse response) {
37-
_categoriesCache = new List<String>();
38-
for (String category in response.data) {
39-
_categoriesCache.add(category);
40-
}
35+
return _http.get(_categoriesUrl).then((HttpResponse response) {
36+
_categoriesCache = response.data;
4137
});
4238
}
4339

Chapter_07/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ dependencies:
55
browser: any
66
js: any
77
shadow_dom: any
8-
98
dev_dependencies:
109
unittest: any

Chapter_07/web/main.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
@Injectables(const[Profiler])
22
library recipe_book;
33

4-
// Temporary, please follow https://github.com/angular/angular.dart/issues/476
5-
@MirrorsUsed(
6-
targets: const ['recipe', 'query_service', 'recipe_book_routing'],
7-
override: '*')
8-
import 'dart:mirrors';
9-
104
import 'package:angular/angular.dart';
115
import 'package:logging/logging.dart';
126

@@ -19,6 +13,11 @@ import 'package:angular_dart_demo/routing/recipe_book_router.dart';
1913
import 'package:angular_dart_demo/component/view_recipe_component.dart';
2014
import 'package:angular_dart_demo/component/search_recipe_component.dart';
2115

16+
// Temporary, please follow https://github.com/angular/angular.dart/issues/476
17+
@MirrorsUsed(
18+
targets: const ['recipe', 'query_service', 'recipe_book_routing'],
19+
override: '*')
20+
import 'dart:mirrors';
2221

2322
// During development it's easier to use dynamic parser and injector, so use
2423
// initializer-dev.dart instead. Before using initializer-prod.dart make sure

Chapter_07/web/style.css

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
[ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
2-
display: none !important;
3-
}
4-
5-
ul {
6-
list-style-type: none;
7-
}
8-
91
a {
102
text-decoration: none;
113
}
124

135
.extra-space {
146
padding-left: 10px;
157
}
8+
9+
[ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
10+
display: none !important;
11+
}
12+
13+
ul {
14+
list-style-type: none;
15+
}

Chapter_07/web/view/addRecipe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<div>
22
<h3>Add recipe</h3>
3-
Now it's your turn. Write some code to add a new recipe
3+
Now it's your turn. Write some code to add a new recipe.
44
</div>

Chapter_07/web/view/editRecipe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<div>
22
<h3>Edit recipe</h3>
3-
Now it's your turn. Write some code to edit the recipe
3+
Now it's your turn. Write some code to edit the recipe.
44
</div>

0 commit comments

Comments
 (0)