OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 | |
6 // Demonstrates: | |
7 // list, maps, random, strings, string interpolation, cascade, fat arrow, | |
8 // named constructors. | |
9 // optional parameters. | |
10 // a class | |
11 // getters, setters | |
12 // httprequest, JSON | |
13 // local storage | |
14 // static class-level methods/fields | |
15 // top-level variable and functions | |
16 // typecasting with 'as' | |
17 // futures | |
18 | |
19 import 'dart:html'; | |
20 import 'dart:async'; | |
21 import 'dart:math'; | |
22 import 'dart:convert'; | |
23 | |
24 final String TREASUREKEY = 'pirateName'; | |
25 | |
26 void main() { | |
27 InputElement inputField = query('#inputName')..onInput.listen(updateBadge); | |
28 ButtonElement genButton = query('#generateButton')..onClick.listen(generateBad ge); | |
29 | |
30 PirateName.readyThePirates() | |
31 .then((obj) { | |
32 inputField.disabled = false; //enable | |
33 genButton.disabled = false; //enable | |
34 badgeName = pirateNameFromStorage; }) | |
35 .catchError((err) { | |
36 print ('Error initializing pirate names: $err'); | |
sethladd
2013/10/22 21:59:17
remove space between print and (
mem
2013/10/22 23:06:24
Done.
| |
37 query('#badgeName').text = 'Arrr! No names.'; | |
38 }); | |
39 } | |
40 | |
41 get pirateNameFromStorage { | |
sethladd
2013/10/22 21:59:17
add return type annotation
mem
2013/10/22 23:06:24
Done.
| |
42 String storedName = window.localStorage[TREASUREKEY]; | |
43 if (storedName != null) { | |
44 return new PirateName.fromJSON(storedName); | |
45 } | |
sethladd
2013/10/22 21:59:17
add explicit return null for else
mem
2013/10/22 23:06:24
Done.
| |
46 } | |
47 | |
48 set badgeName(PirateName newName) { | |
49 query('#badgeName').text = newName.pirateName; | |
sethladd
2013/10/22 21:59:17
you have query('#badgeName') twice in the code. pl
mem
2013/10/22 23:06:24
Done.
| |
50 window.localStorage[TREASUREKEY] = newName.toJsonString(); | |
51 } | |
52 | |
53 void updateBadge(Event e) { | |
54 String inputName = (e.target as InputElement).value; | |
55 ButtonElement genButton = query('#generateButton'); | |
sethladd
2013/10/22 21:59:17
put this variable into top level so you don't need
mem
2013/10/22 23:06:24
Done.
| |
56 | |
57 badgeName = new PirateName(firstName: inputName); | |
58 if (inputName.trim().isEmpty) { | |
59 genButton..disabled = false | |
60 ..text = 'Generate badge'; | |
61 } else { | |
62 genButton..disabled = true | |
63 ..text = 'Arrr! Remove the text!'; | |
64 } | |
65 } | |
66 | |
67 void generateBadge(Event e) { | |
sethladd
2013/10/22 21:59:17
I don't see this called from anywhere
mem
2013/10/22 23:06:24
this is the event handler for the button.
| |
68 badgeName = new PirateName(); | |
69 } | |
70 | |
71 class PirateName { | |
72 | |
73 static final Random indexGen = new Random(); | |
74 | |
75 static List<String> names = []; | |
76 static List<String> appellations = []; | |
77 | |
78 String _firstName; | |
79 String _appellation; | |
80 | |
81 String get pirateName => '$_firstName the $_appellation'; | |
82 | |
83 String toString() => pirateName; | |
84 | |
85 PirateName({String firstName, String appellation}) { | |
sethladd
2013/10/22 21:59:17
typically constructor goes after the fields
mem
2013/10/22 23:06:24
Done.
| |
86 if (firstName == null) { | |
87 _firstName = names[indexGen.nextInt(names.length)]; | |
88 } else { | |
89 _firstName = firstName; | |
90 } | |
91 if (appellation == null) { | |
92 _appellation = appellations[indexGen.nextInt(appellations.length)]; | |
93 } else { | |
94 _appellation = appellation; | |
95 } | |
96 } | |
97 | |
98 String toJsonString() => '{ "f": "$_firstName", "a": "$_appellation" } '; | |
sethladd
2013/10/22 21:59:17
then move this down under constructors
mem
2013/10/22 23:06:24
Done.
| |
99 | |
100 PirateName.fromJSON(String jsonString) { | |
101 Map storedName = JSON.decode(jsonString); | |
102 _firstName = storedName['f']; | |
103 _appellation = storedName['a']; | |
104 } | |
105 | |
106 static Future readyThePirates() { | |
107 var path = 'piratenames.json'; | |
sethladd
2013/10/22 21:59:17
use String instead of var, since you use type anno
| |
108 return HttpRequest.getString(path) | |
109 .then(_parsePirateNamesFromJSON); | |
110 } | |
111 | |
112 static _parsePirateNamesFromJSON(String jsonString) { | |
113 Map pirateNames = JSON.decode(jsonString); | |
114 names = pirateNames['names']; | |
115 appellations = pirateNames['appellations']; | |
116 } | |
117 } | |
OLD | NEW |