Skip to content

Commit b8899d1

Browse files
committed
Merge pull request dart-archive#69 from chalin/patch-chap3-comp
[chap3] maxRating() made more robust and component class comment updated
2 parents 74d117d + bccb9cb commit b8899d1

File tree

10 files changed

+64
-76
lines changed

10 files changed

+64
-76
lines changed

Chapter_03/lib/rating/rating_component.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library rating;
22

33
import 'package:angular/angular.dart';
44

5-
/* Use the NgComponent annotation to indicate that this class is an
6-
* Angular Component.
5+
/* Use the @NgComponent annotation to indicate that this class is an
6+
* Angular component.
77
*
88
* The selector field defines the CSS selector that will trigger the
99
* component. Typically, the CSS selector is an element name.
@@ -16,9 +16,10 @@ import 'package:angular/angular.dart';
1616
* The publishAs field specifies that the component instance should be
1717
* assigned to the current scope under the name specified.
1818
*
19-
* The map field publishes the list of attributes that can be set on
19+
* The class field and setter annotated with @NgTwoWay and @NgAttr,
20+
* respectively, identify the attributes that can be set on
2021
* the component. Users of this component will specify these attributes
21-
* in the html tag that is used to create the component. For example:
22+
* in the HTML tag that is used to create the component. For example:
2223
*
2324
* <rating max-rating="5" rating="mycontrol.rating">
2425
*
@@ -38,14 +39,17 @@ class RatingComponent {
3839
static const String _starOnClass = "star-on";
3940
static const String _starOffClass = "star-off";
4041

42+
static final int DEFAULT_MAX = 5;
43+
4144
List<int> stars = [];
4245

4346
@NgTwoWay('rating')
4447
int rating;
4548

4649
@NgAttr('max-rating')
4750
set maxRating(String value) {
48-
var count = value == null ? 5 : int.parse(value);
51+
var count = value == null ? DEFAULT_MAX :
52+
int.parse(value, onError: (_) => DEFAULT_MAX);
4953
stars = new List.generate(count, (i) => i+1);
5054
}
5155

@@ -58,10 +62,6 @@ class RatingComponent {
5862
}
5963

6064
void handleClick(int star) {
61-
if (star == 1 && rating == 1) {
62-
rating = 0;
63-
} else {
64-
rating = star;
65-
}
65+
rating = (star == 1 && rating == 1) ? 0 : star;
6666
}
6767
}

Chapter_03/web/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ class MyAppModule extends Module {
1919
}
2020
}
2121

22-
main() {
22+
void main() {
2323
ngBootstrap(module: new MyAppModule());
2424
}

Chapter_04/lib/rating/rating_component.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library rating;
22

33
import 'package:angular/angular.dart';
44

5-
/* Use the NgComponent annotation to indicate that this class is an
6-
* Angular Component.
5+
/* Use the @NgComponent annotation to indicate that this class is an
6+
* Angular component.
77
*
88
* The selector field defines the CSS selector that will trigger the
99
* component. Typically, the CSS selector is an element name.
@@ -16,9 +16,10 @@ import 'package:angular/angular.dart';
1616
* The publishAs field specifies that the component instance should be
1717
* assigned to the current scope under the name specified.
1818
*
19-
* The map field publishes the list of attributes that can be set on
19+
* The class field and setter annotated with @NgTwoWay and @NgAttr,
20+
* respectively, identify the attributes that can be set on
2021
* the component. Users of this component will specify these attributes
21-
* in the html tag that is used to create the component. For example:
22+
* in the HTML tag that is used to create the component. For example:
2223
*
2324
* <rating max-rating="5" rating="mycontrol.rating">
2425
*
@@ -38,18 +39,18 @@ class RatingComponent {
3839
static const String _starOnClass = "star-on";
3940
static const String _starOffClass = "star-off";
4041

42+
static final int DEFAULT_MAX = 5;
43+
4144
List<int> stars = [];
4245

4346
@NgTwoWay('rating')
4447
int rating;
4548

4649
@NgAttr('max-rating')
4750
set maxRating(String value) {
48-
stars = [];
49-
var count = value == null ? 5 : int.parse(value);
50-
for(var i=1; i <= count; i++) {
51-
stars.add(i);
52-
}
51+
var count = value == null ? DEFAULT_MAX :
52+
int.parse(value, onError: (_) => DEFAULT_MAX);
53+
stars = new List.generate(count, (i) => i+1);
5354
}
5455

5556
String starClass(int star) {
@@ -61,10 +62,6 @@ class RatingComponent {
6162
}
6263

6364
void handleClick(int star) {
64-
if (star == 1 && rating == 1) {
65-
rating = 0;
66-
} else {
67-
rating = star;
68-
}
65+
rating = (star == 1 && rating == 1) ? 0 : star;
6966
}
7067
}

Chapter_04/web/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ class MyAppModule extends Module {
2323
}
2424
}
2525

26-
main() {
26+
void main() {
2727
ngBootstrap(module: new MyAppModule());
2828
}

Chapter_05/lib/rating/rating_component.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library rating;
22

33
import 'package:angular/angular.dart';
44

5-
/* Use the NgComponent annotation to indicate that this class is an
6-
* Angular Component.
5+
/* Use the @NgComponent annotation to indicate that this class is an
6+
* Angular component.
77
*
88
* The selector field defines the CSS selector that will trigger the
99
* component. Typically, the CSS selector is an element name.
@@ -16,9 +16,10 @@ import 'package:angular/angular.dart';
1616
* The publishAs field specifies that the component instance should be
1717
* assigned to the current scope under the name specified.
1818
*
19-
* The map field publishes the list of attributes that can be set on
19+
* The class field and setter annotated with @NgTwoWay and @NgAttr,
20+
* respectively, identify the attributes that can be set on
2021
* the component. Users of this component will specify these attributes
21-
* in the html tag that is used to create the component. For example:
22+
* in the HTML tag that is used to create the component. For example:
2223
*
2324
* <rating max-rating="5" rating="mycontrol.rating">
2425
*
@@ -38,18 +39,18 @@ class RatingComponent {
3839
static const String _starOnClass = "star-on";
3940
static const String _starOffClass = "star-off";
4041

42+
static final int DEFAULT_MAX = 5;
43+
4144
List<int> stars = [];
4245

4346
@NgTwoWay('rating')
4447
int rating;
4548

4649
@NgAttr('max-rating')
4750
set maxRating(String value) {
48-
stars = [];
49-
var count = value == null ? 5 : int.parse(value);
50-
for(var i=1; i <= count; i++) {
51-
stars.add(i);
52-
}
51+
var count = value == null ? DEFAULT_MAX :
52+
int.parse(value, onError: (_) => DEFAULT_MAX);
53+
stars = new List.generate(count, (i) => i+1);
5354
}
5455

5556
String starClass(int star) {
@@ -61,10 +62,6 @@ class RatingComponent {
6162
}
6263

6364
void handleClick(int star) {
64-
if (star == 1 && rating == 1) {
65-
rating = 0;
66-
} else {
67-
rating = star;
68-
}
65+
rating = (star == 1 && rating == 1) ? 0 : star;
6966
}
7067
}

Chapter_05/web/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class MyAppModule extends Module {
2525
}
2626
}
2727

28-
main() {
28+
void main() {
2929
ngBootstrap(module: new MyAppModule());
3030
}

Chapter_06/lib/rating/rating_component.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library rating;
22

33
import 'package:angular/angular.dart';
44

5-
/* Use the NgComponent annotation to indicate that this class is an
6-
* Angular Component.
5+
/* Use the @NgComponent annotation to indicate that this class is an
6+
* Angular component.
77
*
88
* The selector field defines the CSS selector that will trigger the
99
* component. Typically, the CSS selector is an element name.
@@ -16,9 +16,10 @@ import 'package:angular/angular.dart';
1616
* The publishAs field specifies that the component instance should be
1717
* assigned to the current scope under the name specified.
1818
*
19-
* The map field publishes the list of attributes that can be set on
19+
* The class field and setter annotated with @NgTwoWay and @NgAttr,
20+
* respectively, identify the attributes that can be set on
2021
* the component. Users of this component will specify these attributes
21-
* in the html tag that is used to create the component. For example:
22+
* in the HTML tag that is used to create the component. For example:
2223
*
2324
* <rating max-rating="5" rating="mycontrol.rating">
2425
*
@@ -38,18 +39,18 @@ class RatingComponent {
3839
static const String _starOnClass = "star-on";
3940
static const String _starOffClass = "star-off";
4041

42+
static final int DEFAULT_MAX = 5;
43+
4144
List<int> stars = [];
4245

4346
@NgTwoWay('rating')
4447
int rating;
4548

4649
@NgAttr('max-rating')
4750
set maxRating(String value) {
48-
stars = [];
49-
var count = value == null ? 5 : int.parse(value);
50-
for(var i=1; i <= count; i++) {
51-
stars.add(i);
52-
}
51+
var count = value == null ? DEFAULT_MAX :
52+
int.parse(value, onError: (_) => DEFAULT_MAX);
53+
stars = new List.generate(count, (i) => i+1);
5354
}
5455

5556
String starClass(int star) {
@@ -61,10 +62,6 @@ class RatingComponent {
6162
}
6263

6364
void handleClick(int star) {
64-
if (star == 1 && rating == 1) {
65-
rating = 0;
66-
} else {
67-
rating = star;
68-
}
65+
rating = (star == 1 && rating == 1) ? 0 : star;
6966
}
7067
}

Chapter_06/web/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MyAppModule extends Module {
3838
}
3939
}
4040

41-
main() {
41+
void main() {
4242
Logger.root.level = Level.FINEST;
4343
Logger.root.onRecord.listen((LogRecord r) { print(r.message); });
4444
ngBootstrap(module: new MyAppModule());

Chapter_07/lib/rating/rating_component.dart

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library rating;
22

33
import 'package:angular/angular.dart';
44

5-
/* Use the NgComponent annotation to indicate that this class is an
6-
* Angular Component.
5+
/* Use the @NgComponent annotation to indicate that this class is an
6+
* Angular component.
77
*
88
* The selector field defines the CSS selector that will trigger the
99
* component. Typically, the CSS selector is an element name.
@@ -16,13 +16,14 @@ import 'package:angular/angular.dart';
1616
* The publishAs field specifies that the component instance should be
1717
* assigned to the current scope under the name specified.
1818
*
19-
* The map field publishes the list of attributes that can be set on
19+
* The class field and setter annotated with @NgTwoWay and @NgAttr,
20+
* respectively, identify the attributes that can be set on
2021
* the component. Users of this component will specify these attributes
21-
* in the html tag that is used to create the component. For example:
22+
* in the HTML tag that is used to create the component. For example:
2223
*
2324
* <rating max-rating="5" rating="mycontrol.rating">
2425
*
25-
* The compnoent's public fields are available for data binding from the
26+
* The component's public fields are available for data binding from the
2627
* component's view. Similarly, the component's public methods can be
2728
* invoked from the component's view.
2829
*/
@@ -33,23 +34,23 @@ import 'package:angular/angular.dart';
3334
publishAs: 'ctrl'
3435
)
3536
class RatingComponent {
36-
String _starOnChar = "\u2605";
37-
String _starOffChar = "\u2606";
38-
String _starOnClass = "star-on";
39-
String _starOffClass = "star-off";
37+
static const String _starOnChar = "\u2605";
38+
static const String _starOffChar = "\u2606";
39+
static const String _starOnClass = "star-on";
40+
static const String _starOffClass = "star-off";
4041

42+
static final int DEFAULT_MAX = 5;
43+
4144
List<int> stars = [];
4245

4346
@NgTwoWay('rating')
4447
int rating;
4548

4649
@NgAttr('max-rating')
4750
set maxRating(String value) {
48-
stars = [];
49-
var count = value == null ? 5 : int.parse(value);
50-
for(var i=1; i <= count; i++) {
51-
stars.add(i);
52-
}
51+
var count = value == null ? DEFAULT_MAX :
52+
int.parse(value, onError: (_) => DEFAULT_MAX);
53+
stars = new List.generate(count, (i) => i+1);
5354
}
5455

5556
String starClass(int star) {
@@ -61,10 +62,6 @@ class RatingComponent {
6162
}
6263

6364
void handleClick(int star) {
64-
if (star == 1 && rating == 1) {
65-
rating = 0;
66-
} else {
67-
rating = star;
68-
}
65+
rating = (star == 1 && rating == 1) ? 0 : star;
6966
}
7067
}

Chapter_07/web/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MyAppModule extends Module {
5252
}
5353
}
5454

55-
main() {
55+
void main() {
5656
Logger.root.level = Level.FINEST;
5757
Logger.root.onRecord.listen((LogRecord r) { print(r.message); });
5858
ngBootstrap(module: new MyAppModule(), injectorFactory: init.createInjector);

0 commit comments

Comments
 (0)