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: | |
sethladd
2013/10/22 20:19:06
love the comments in here. Let's link to a doc fro
mem
2013/10/22 21:39:43
I can link to the doc once it has a home. Also, it
| |
7 // list, maps, random, strings, string interpolation, cascade, fat arrow, | |
8 // named constructors. | |
9 // optional parameters. | |
10 // a class | |
11 // getters, | |
12 // httprequest, JSON | |
13 // local storage | |
14 // 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 query('#inputName').onInput.listen(updateBadge); | |
28 query('#generateButton').onClick.listen(generateBadge); | |
29 query('#badgeName').onChange.listen(storePirateName); | |
sethladd
2013/10/22 20:19:06
my mistake, this doesn't work. Can we try a setter
mem
2013/10/22 21:39:43
Done.
| |
30 | |
31 PirateName.initialize().then((e) { | |
32 (query('#generateButton') as ButtonElement).disabled = false; | |
sethladd
2013/10/22 20:19:06
de-dupe these queries and create variables above
mem
2013/10/22 21:39:43
Done.
| |
33 (query('#inputName') as InputElement).disabled = false; | |
sethladd
2013/10/22 20:19:06
can the HTML start as disabled?
mem
2013/10/22 21:39:43
they *DO* start as disabled.
this is enabling them
| |
34 String storedName = window.localStorage[TREASUREKEY]; | |
35 if (storedName != null) { | |
36 query('#badgeName').text = new PirateName.fromJSON(window.localStorage[TRE ASUREKEY]).pirateName; | |
sethladd
2013/10/22 20:19:06
create a getter for new PirateName.fromJSON(window
mem
2013/10/22 21:39:43
Done.
| |
37 } | |
38 }).catchError((error) { | |
39 print ('Error reading JSON file: $error'); | |
sethladd
2013/10/22 20:29:15
the error is more generic than "reading JSON"
mem
2013/10/22 21:39:43
Done.
| |
40 query('#badgeName').text = 'Arrr! No names.'; | |
sethladd
2013/10/22 20:29:15
pull query out from here
mem
2013/10/22 21:39:43
Done.
| |
41 } ); | |
42 } | |
43 | |
44 void updateBadge(Event e) { | |
45 String inputName = (e.target as InputElement).value; | |
46 ButtonElement genButton = query('#generateButton'); | |
47 | |
48 query('#badgeName').text = new PirateName(firstName: inputName).pirateName; | |
sethladd
2013/10/22 20:29:15
if you use query like this through the code, you c
mem
2013/10/22 21:39:43
Done.
| |
49 if (inputName == '') { | |
sethladd
2013/10/22 20:29:15
inputName.trim().isEmpty
mem
2013/10/22 21:39:43
Done.
| |
50 genButton..disabled = false | |
51 ..text = 'Generate badge'; | |
52 } else { | |
53 genButton..disabled = true | |
54 ..text = 'Arrr! Remove the text!'; | |
55 } | |
56 } | |
57 | |
58 //NOTE: this is not getting called. no change event is getting fired | |
59 void storePirateName(Event e) { | |
60 window.localStorage[TREASUREKEY] = JSON.encode((e.target as SpanElement).text) ; | |
61 } | |
62 | |
63 void generateBadge(Event e) { | |
64 query('#badgeName').text = new PirateName().pirateName; | |
65 } | |
66 | |
67 class PirateName { | |
68 | |
69 static final Random indexGen = new Random(); | |
70 | |
71 String _firstName; | |
72 String _appellation; | |
73 | |
74 String get pirateName => '$_firstName the $_appellation'; | |
75 | |
76 String toString() => pirateName; | |
77 | |
78 PirateName({String firstName}) { | |
sethladd
2013/10/22 20:29:15
why not appellation ?
mem
2013/10/22 21:39:43
Done.
| |
79 if (firstName == null) { | |
80 _firstName = names[indexGen.nextInt(names.length)]; | |
81 } else { | |
82 _firstName = firstName; | |
83 } | |
84 _appellation = appellations[indexGen.nextInt(appellations.length)]; | |
85 } | |
86 | |
87 PirateName.fromJSON(String jsonString){ | |
sethladd
2013/10/22 20:29:15
space before {
mem
2013/10/22 21:39:43
Done.
| |
88 String storedName = JSON.decode(jsonString); | |
89 List splitName = storedName.split(' '); | |
sethladd
2013/10/22 20:29:15
why not store the first name and appellation as se
mem
2013/10/22 21:39:43
Done.
mem
2013/10/22 21:39:43
Done.
mem
2013/10/22 21:39:43
Done.
| |
90 _firstName = splitName[0]; | |
91 _appellation = splitName[2]; | |
92 } | |
93 | |
94 static List<String> names = []; | |
sethladd
2013/10/22 20:29:15
I think these should go up where the other statics
mem
2013/10/22 21:39:43
Done.
| |
95 static List<String> appellations = []; | |
96 | |
97 static Future initialize() { | |
sethladd
2013/10/22 20:29:15
initialize is really generic. is there a more desc
mem
2013/10/22 21:39:43
Done.
| |
98 return makeRequest(); | |
99 } | |
100 | |
101 static Future makeRequest(/*Event e*/) { | |
sethladd
2013/10/22 20:29:15
how about moving the contents from this method up
mem
2013/10/22 21:39:43
Done.
| |
102 var path = 'piratenames.json'; | |
103 return HttpRequest.getString(path) | |
104 .then(parsePirateNamesFromJSON); | |
105 } | |
106 | |
107 static parsePirateNamesFromJSON(String jsonString) { | |
sethladd
2013/10/22 20:29:15
make this a private method
mem
2013/10/22 21:39:43
Done.
| |
108 Map pirateNames = JSON.decode(jsonString); | |
109 names = pirateNames['names']; | |
110 appellations = pirateNames['appellations']; | |
111 } | |
112 } | |
OLD | NEW |