From 6339ad812c881835d2cbb7ee02ac731b34206571 Mon Sep 17 00:00:00 2001 From: Darren Carlton Date: Mon, 4 May 2015 14:38:27 -0400 Subject: [PATCH 1/2] Updating the tutorial to Dart 1.9+, which introduces an additional pattern for asynchronous methods. --- Chapter_01/pubspec.lock | 26 +++++----- Chapter_01/pubspec.yaml | 4 +- Chapter_02/pubspec.lock | 24 ++++----- Chapter_02/pubspec.yaml | 4 +- Chapter_03/pubspec.lock | 16 +++--- Chapter_03/pubspec.yaml | 4 +- Chapter_04/lib/component/rating.dart | 6 ++- Chapter_04/pubspec.lock | 16 +++--- Chapter_04/pubspec.yaml | 4 +- Chapter_05/lib/component/rating.dart | 9 ++-- Chapter_05/lib/component/recipe_book.dart | 46 +++++++++-------- Chapter_05/pubspec.lock | 16 +++--- Chapter_05/pubspec.yaml | 4 +- Chapter_06/lib/component/rating.dart | 6 ++- Chapter_06/lib/component/recipe_book.dart | 29 ++++++----- Chapter_06/lib/service/query.dart | 63 ++++++++++++++--------- Chapter_06/pubspec.lock | 16 +++--- Chapter_06/pubspec.yaml | 4 +- package.json | 4 +- 19 files changed, 164 insertions(+), 137 deletions(-) diff --git a/Chapter_01/pubspec.lock b/Chapter_01/pubspec.lock index a0cc53f..4dd03ec 100644 --- a/Chapter_01/pubspec.lock +++ b/Chapter_01/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.7+2" + version: "0.2.8" collection: description: collection source: hosted - version: "0.9.4" + version: "1.1.0" csslib: description: csslib source: hosted @@ -56,27 +56,27 @@ packages: intl: description: intl source: hosted - version: "0.11.12" + version: "0.8.10+4" logging: description: logging source: hosted version: "0.9.3" + meta: + description: meta + source: hosted + version: "0.8.8" observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.0+2" path: description: path source: hosted - version: "1.3.4" + version: "1.3.5" perf_api: description: perf_api source: hosted version: "0.0.9" - petitparser: - description: petitparser - source: hosted - version: "1.3.7" pool: description: pool source: hosted @@ -92,7 +92,7 @@ packages: source_maps: description: source_maps source: hosted - version: "0.10.0+2" + version: "0.10.1" source_span: description: source_span source: hosted @@ -100,7 +100,7 @@ packages: stack_trace: description: stack_trace source: hosted - version: "1.2.4" + version: "1.3.2" utf: description: utf source: hosted diff --git a/Chapter_01/pubspec.yaml b/Chapter_01/pubspec.yaml index 35d04f9..7440726 100644 --- a/Chapter_01/pubspec.yaml +++ b/Chapter_01/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: ">=0.10.0+2 <0.11.0" transformers: diff --git a/Chapter_02/pubspec.lock b/Chapter_02/pubspec.lock index e94b364..cc06d09 100644 --- a/Chapter_02/pubspec.lock +++ b/Chapter_02/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.5" + version: "0.2.9" collection: description: collection source: hosted - version: "0.9.4" + version: "1.1.0" csslib: description: csslib source: hosted @@ -40,7 +40,7 @@ packages: di: description: di source: hosted - version: "3.3.3" + version: "3.3.4" html5lib: description: html5lib source: hosted @@ -52,7 +52,7 @@ packages: intl: description: intl source: hosted - version: "0.11.9" + version: "0.8.10+4" logging: description: logging source: hosted @@ -61,6 +61,10 @@ packages: description: matcher source: hosted version: "0.11.1" + meta: + description: meta + source: hosted + version: "0.8.8" mock: description: mock source: hosted @@ -68,7 +72,7 @@ packages: observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.1" path: description: path source: hosted @@ -77,10 +81,6 @@ packages: description: perf_api source: hosted version: "0.0.9" - petitparser: - description: petitparser - source: hosted - version: "1.2.2" pool: description: pool source: hosted @@ -92,7 +92,7 @@ packages: smoke: description: smoke source: hosted - version: "0.3.1" + version: "0.3.3" source_maps: description: source_maps source: hosted diff --git a/Chapter_02/pubspec.yaml b/Chapter_02/pubspec.yaml index 17aa22b..d5a2323 100644 --- a/Chapter_02/pubspec.yaml +++ b/Chapter_02/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: ">=0.10.0+2 <0.11.0" dev_dependencies: diff --git a/Chapter_03/pubspec.lock b/Chapter_03/pubspec.lock index 45acad8..e419d16 100644 --- a/Chapter_03/pubspec.lock +++ b/Chapter_03/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.5" + version: "0.2.9" collection: description: collection source: hosted - version: "0.9.1" + version: "1.1.0" csslib: description: csslib source: hosted @@ -40,7 +40,7 @@ packages: di: description: di source: hosted - version: "3.3.3" + version: "3.3.4" html5lib: description: html5lib source: hosted @@ -56,7 +56,7 @@ packages: logging: description: logging source: hosted - version: "0.9.1+1" + version: "0.9.3" matcher: description: matcher source: hosted @@ -72,7 +72,7 @@ packages: observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.1" path: description: path source: hosted @@ -92,7 +92,7 @@ packages: smoke: description: smoke source: hosted - version: "0.3.1" + version: "0.3.3" source_maps: description: source_maps source: hosted diff --git a/Chapter_03/pubspec.yaml b/Chapter_03/pubspec.yaml index d8359c4..eca5b00 100644 --- a/Chapter_03/pubspec.yaml +++ b/Chapter_03/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: ">=0.10.0+2 <0.11.0" dev_dependencies: diff --git a/Chapter_04/lib/component/rating.dart b/Chapter_04/lib/component/rating.dart index ebe1a81..f7eec0f 100644 --- a/Chapter_04/lib/component/rating.dart +++ b/Chapter_04/lib/component/rating.dart @@ -52,9 +52,11 @@ class RatingComponent { stars = new List.generate(count, (i) => i + 1); } - String starClass(int star) => star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; + String starClass(int star) => + rating == null || star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; - String starChar(int star) => star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; + String starChar(int star) => + rating == null || star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; void handleClick(int star) { rating = (star == 1 && rating == 1) ? 0 : star; diff --git a/Chapter_04/pubspec.lock b/Chapter_04/pubspec.lock index 45acad8..e419d16 100644 --- a/Chapter_04/pubspec.lock +++ b/Chapter_04/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.5" + version: "0.2.9" collection: description: collection source: hosted - version: "0.9.1" + version: "1.1.0" csslib: description: csslib source: hosted @@ -40,7 +40,7 @@ packages: di: description: di source: hosted - version: "3.3.3" + version: "3.3.4" html5lib: description: html5lib source: hosted @@ -56,7 +56,7 @@ packages: logging: description: logging source: hosted - version: "0.9.1+1" + version: "0.9.3" matcher: description: matcher source: hosted @@ -72,7 +72,7 @@ packages: observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.1" path: description: path source: hosted @@ -92,7 +92,7 @@ packages: smoke: description: smoke source: hosted - version: "0.3.1" + version: "0.3.3" source_maps: description: source_maps source: hosted diff --git a/Chapter_04/pubspec.yaml b/Chapter_04/pubspec.yaml index e2ecb99..958b183 100644 --- a/Chapter_04/pubspec.yaml +++ b/Chapter_04/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: ">=0.10.0+2 <0.11.0" dev_dependencies: diff --git a/Chapter_05/lib/component/rating.dart b/Chapter_05/lib/component/rating.dart index ebe1a81..34e5ee9 100644 --- a/Chapter_05/lib/component/rating.dart +++ b/Chapter_05/lib/component/rating.dart @@ -32,6 +32,7 @@ import 'package:angular/angular.dart'; templateUrl: 'rating.html', cssUrl: 'rating.css') class RatingComponent { + static const String _STAR_ON_CHAR = "\u2605"; static const String _STAR_OFF_CHAR = "\u2606"; static const String _STAR_ON_CLASS = "star-on"; @@ -52,11 +53,13 @@ class RatingComponent { stars = new List.generate(count, (i) => i + 1); } - String starClass(int star) => star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; + String starClass(int star) => + rating == null || star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; - String starChar(int star) => star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; + String starChar(int star) => + rating == null || star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; void handleClick(int star) { rating = (star == 1 && rating == 1) ? 0 : star; } -} +} \ No newline at end of file diff --git a/Chapter_05/lib/component/recipe_book.dart b/Chapter_05/lib/component/recipe_book.dart index b9cb3da..5cc7022 100644 --- a/Chapter_05/lib/component/recipe_book.dart +++ b/Chapter_05/lib/component/recipe_book.dart @@ -1,6 +1,8 @@ library recipe_book_component; import 'package:angular/angular.dart'; +import 'dart:async'; + import 'package:tutorial/tooltip/tooltip.dart' show TooltipModel; import 'package:tutorial/recipe.dart'; @@ -61,35 +63,35 @@ class RecipeBookComponent { nameFilterString = ""; } - void _loadData() { + Future _loadData() async { recipesLoaded = false; categoriesLoaded = false; - _http.get('recipes.json') - .then((HttpResponse response) { - print(response); - recipes = response.data.map((d) => new Recipe.fromJson(d)).toList(); - recipesLoaded = true; - }) - .catchError((e) { + + try { + var response = await _http.get('recipes.json'); + print(response); + recipes = response.data.map((d) => new Recipe.fromJson(d)).toList(); + recipesLoaded = true; + } on Error catch (e) { print(e); recipesLoaded = false; message = ERROR_MESSAGE; - }); - - _http.get('categories.json') - .then((HttpResponse response) { - print(response); - for (String category in response.data) { - categoryFilterMap[category] = false; - } - categories.clear(); - categories.addAll(categoryFilterMap.keys); - categoriesLoaded = true; - }) - .catchError((e) { + }; + + try { + var response = await _http.get('categories.json'); + print(response); + + for (String category in response.data) { + categoryFilterMap[category] = false; + } + categories.clear(); + categories.addAll(categoryFilterMap.keys); + categoriesLoaded = true; + } on Error catch (e) { print(e); categoriesLoaded = false; message = ERROR_MESSAGE; - }); + } } } diff --git a/Chapter_05/pubspec.lock b/Chapter_05/pubspec.lock index 45acad8..e419d16 100644 --- a/Chapter_05/pubspec.lock +++ b/Chapter_05/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.5" + version: "0.2.9" collection: description: collection source: hosted - version: "0.9.1" + version: "1.1.0" csslib: description: csslib source: hosted @@ -40,7 +40,7 @@ packages: di: description: di source: hosted - version: "3.3.3" + version: "3.3.4" html5lib: description: html5lib source: hosted @@ -56,7 +56,7 @@ packages: logging: description: logging source: hosted - version: "0.9.1+1" + version: "0.9.3" matcher: description: matcher source: hosted @@ -72,7 +72,7 @@ packages: observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.1" path: description: path source: hosted @@ -92,7 +92,7 @@ packages: smoke: description: smoke source: hosted - version: "0.3.1" + version: "0.3.3" source_maps: description: source_maps source: hosted diff --git a/Chapter_05/pubspec.yaml b/Chapter_05/pubspec.yaml index e2ecb99..958b183 100644 --- a/Chapter_05/pubspec.yaml +++ b/Chapter_05/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: ">=0.10.0+2 <0.11.0" dev_dependencies: diff --git a/Chapter_06/lib/component/rating.dart b/Chapter_06/lib/component/rating.dart index ebe1a81..3eeee39 100644 --- a/Chapter_06/lib/component/rating.dart +++ b/Chapter_06/lib/component/rating.dart @@ -52,9 +52,11 @@ class RatingComponent { stars = new List.generate(count, (i) => i + 1); } - String starClass(int star) => star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; + String starClass(int star) => + rating == null || star > rating ? _STAR_OFF_CLASS : _STAR_ON_CLASS; - String starChar(int star) => star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; + String starChar(int star) => + rating == null || star > rating ? _STAR_OFF_CHAR : _STAR_ON_CHAR; void handleClick(int star) { rating = (star == 1 && rating == 1) ? 0 : star; diff --git a/Chapter_06/lib/component/recipe_book.dart b/Chapter_06/lib/component/recipe_book.dart index 8e4d4db..b6ec59b 100644 --- a/Chapter_06/lib/component/recipe_book.dart +++ b/Chapter_06/lib/component/recipe_book.dart @@ -1,6 +1,8 @@ library recipe_book_component; import 'package:angular/angular.dart'; +import 'dart:async'; + import 'package:tutorial/tooltip/tooltip.dart' show TooltipModel; import 'package:tutorial/service/recipe.dart'; import 'package:tutorial/service/query.dart'; @@ -62,31 +64,30 @@ class RecipeBookComponent { return _tooltip[recipe]; // recipe.tooltip } - void _loadData() { - queryService.getAllRecipes() - .then((Map allRecipes) { - _recipeMap = allRecipes; - _allRecipes = _recipeMap.values.toList(); - recipesLoaded = true; - }) - .catchError((e) { + Future _loadData() async { + try { + var allRecipes = await queryService.getAllRecipes(); + _recipeMap = allRecipes; + _allRecipes = _recipeMap.values.toList(); + recipesLoaded = true; + } on Error catch (e) { print(e); recipesLoaded = false; message = ERROR_MESSAGE; - }); + } + + try { + var allCategories = await queryService.getAllCategories(); - queryService.getAllCategories() - .then((List allCategories) { for (String category in allCategories) { categoryFilterMap[category] = false; } categories.addAll(categoryFilterMap.keys); categoriesLoaded = true; - }) - .catchError((e) { + } on Error catch (e) { print(e); categoriesLoaded = false; message = ERROR_MESSAGE; - }); + } } } diff --git a/Chapter_06/lib/service/query.dart b/Chapter_06/lib/service/query.dart index 1d4a3ce..5d685a7 100644 --- a/Chapter_06/lib/service/query.dart +++ b/Chapter_06/lib/service/query.dart @@ -21,38 +21,53 @@ class QueryService { _loaded = Future.wait([_loadRecipes(), _loadCategories()]); } - Future _loadRecipes() { - return _http.get(_recipesUrl) - .then((HttpResponse response) { - _recipesCache = new Map(); - for (Map recipe in response.data) { - Recipe r = new Recipe.fromJson(recipe); - _recipesCache[r.id] = r; - } - }); + Future _loadRecipes() async { + try { + var response = await _http.get(_recipesUrl); + _recipesCache = new Map(); + + for (Map recipe in response.data) { + Recipe r = new Recipe.fromJson(recipe); + _recipesCache[r.id] = r; + } + } on Error catch(e){ + throw(e); + } } - Future _loadCategories() { - return _http.get(_categoriesUrl).then((HttpResponse response) { + Future _loadCategories() async { + try { + var response = await _http.get(_categoriesUrl); _categoriesCache = response.data; - }); + } on Error catch(e){ + throw(e); + } } - Future getRecipeById(String id) { - return _recipesCache == null - ? _loaded.then((_) => _recipesCache[id]) - : new Future.value(_recipesCache[id]); + Future getRecipeById(String id) async { + try { + if (_recipesCache == null) await _loaded; + return _recipesCache[id]; + } on Error catch(e){ + throw(e); + } } - Future> getAllRecipes() { - return _recipesCache == null - ? _loaded.then((_) => _recipesCache) - : new Future.value(_recipesCache); + Future> getAllRecipes() async { + try { + if (_recipesCache == null) await _loaded; + return _recipesCache; + } on Error catch(e){ + throw(e); + } } - Future> getAllCategories() { - return _categoriesCache == null - ? _loaded.then((_) => _categoriesCache) - : new Future.value(_categoriesCache); + Future> getAllCategories() async { + try { + if (_categoriesCache == null) await _loaded; + return _categoriesCache; + } on Error catch(e){ + throw(e); + } } } diff --git a/Chapter_06/pubspec.lock b/Chapter_06/pubspec.lock index 1a068a9..af35a44 100644 --- a/Chapter_06/pubspec.lock +++ b/Chapter_06/pubspec.lock @@ -4,11 +4,11 @@ packages: analyzer: description: analyzer source: hosted - version: "0.22.4" + version: "0.24.6" angular: description: angular source: hosted - version: "1.1.0" + version: "1.1.2" args: description: args source: hosted @@ -28,11 +28,11 @@ packages: code_transformers: description: code_transformers source: hosted - version: "0.2.5" + version: "0.2.9" collection: description: collection source: hosted - version: "0.9.1" + version: "1.1.0" csslib: description: csslib source: hosted @@ -40,7 +40,7 @@ packages: di: description: di source: hosted - version: "3.3.3" + version: "3.3.4" html5lib: description: html5lib source: hosted @@ -60,7 +60,7 @@ packages: logging: description: logging source: hosted - version: "0.9.1+1" + version: "0.9.3" matcher: description: matcher source: hosted @@ -80,7 +80,7 @@ packages: observe: description: observe source: hosted - version: "0.12.2+1" + version: "0.13.1" path: description: path source: hosted @@ -100,7 +100,7 @@ packages: smoke: description: smoke source: hosted - version: "0.3.1" + version: "0.3.3" source_maps: description: source_maps source: hosted diff --git a/Chapter_06/pubspec.yaml b/Chapter_06/pubspec.yaml index 99262a1..5f6005c 100644 --- a/Chapter_06/pubspec.yaml +++ b/Chapter_06/pubspec.yaml @@ -1,7 +1,7 @@ name: tutorial -version: 1.0.0 +version: 1.1.2 dependencies: - angular: "1.1.0" + angular: "1.1.2" web_components: "0.10.1" browser: any http_server: ">=0.9.3 <0.10.0" diff --git a/package.json b/package.json index ad306cc..0bfe261 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,8 @@ "karma-junit-reporter": "*" }, "devDependencies": { - "karma-chrome-launcher": "^0.1.3" + "karma-chrome-launcher": "~0.1.8", + "karma": "~0.11.14", + "karma-dart": "~0.2.8" } } From 9cce103b8ad13d2555cfa342119caca95dc254a9 Mon Sep 17 00:00:00 2001 From: Darren Carlton Date: Mon, 4 May 2015 17:29:59 -0400 Subject: [PATCH 2/2] Remove semi-colon, as suggested by a reviewer --- Chapter_05/lib/component/recipe_book.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter_05/lib/component/recipe_book.dart b/Chapter_05/lib/component/recipe_book.dart index 5cc7022..24cc02a 100644 --- a/Chapter_05/lib/component/recipe_book.dart +++ b/Chapter_05/lib/component/recipe_book.dart @@ -76,7 +76,7 @@ class RecipeBookComponent { print(e); recipesLoaded = false; message = ERROR_MESSAGE; - }; + } try { var response = await _http.get('categories.json');