Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Unified Diff: src/site/articles/trydart/code/piratebadge.dart

Issue 35913002: Try Dart 1 hour experience first draft (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: trying again to upload chnages. still getting an error Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/site/articles/trydart/code/piratebadge.dart
diff --git a/src/site/articles/trydart/code/piratebadge.dart b/src/site/articles/trydart/code/piratebadge.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ce86b63002b742f2a4b363acf1882656ad62f5e4
--- /dev/null
+++ b/src/site/articles/trydart/code/piratebadge.dart
@@ -0,0 +1,117 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+
+// Demonstrates:
+// list, maps, random, strings, string interpolation, cascade, fat arrow,
+// named constructors.
+// optional parameters.
+// a class
+// getters, setters
+// httprequest, JSON
+// local storage
+// static class-level methods/fields
+// top-level variable and functions
+// typecasting with 'as'
+// futures
+
+import 'dart:html';
+import 'dart:async';
+import 'dart:math';
+import 'dart:convert';
+
+final String TREASUREKEY = 'pirateName';
+
+void main() {
+ InputElement inputField = query('#inputName')..onInput.listen(updateBadge);
+ ButtonElement genButton = query('#generateButton')..onClick.listen(generateBadge);
+
+ PirateName.readyThePirates()
+ .then((obj) {
+ inputField.disabled = false; //enable
+ genButton.disabled = false; //enable
+ badgeName = pirateNameFromStorage; })
+ .catchError((err) {
+ 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.
+ query('#badgeName').text = 'Arrr! No names.';
+ });
+}
+
+get pirateNameFromStorage {
sethladd 2013/10/22 21:59:17 add return type annotation
mem 2013/10/22 23:06:24 Done.
+ String storedName = window.localStorage[TREASUREKEY];
+ if (storedName != null) {
+ return new PirateName.fromJSON(storedName);
+ }
sethladd 2013/10/22 21:59:17 add explicit return null for else
mem 2013/10/22 23:06:24 Done.
+}
+
+set badgeName(PirateName newName) {
+ 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.
+ window.localStorage[TREASUREKEY] = newName.toJsonString();
+}
+
+void updateBadge(Event e) {
+ String inputName = (e.target as InputElement).value;
+ 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.
+
+ badgeName = new PirateName(firstName: inputName);
+ if (inputName.trim().isEmpty) {
+ genButton..disabled = false
+ ..text = 'Generate badge';
+ } else {
+ genButton..disabled = true
+ ..text = 'Arrr! Remove the text!';
+ }
+}
+
+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.
+ badgeName = new PirateName();
+}
+
+class PirateName {
+
+ static final Random indexGen = new Random();
+
+ static List<String> names = [];
+ static List<String> appellations = [];
+
+ String _firstName;
+ String _appellation;
+
+ String get pirateName => '$_firstName the $_appellation';
+
+ String toString() => pirateName;
+
+ 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.
+ if (firstName == null) {
+ _firstName = names[indexGen.nextInt(names.length)];
+ } else {
+ _firstName = firstName;
+ }
+ if (appellation == null) {
+ _appellation = appellations[indexGen.nextInt(appellations.length)];
+ } else {
+ _appellation = appellation;
+ }
+ }
+
+ 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.
+
+ PirateName.fromJSON(String jsonString) {
+ Map storedName = JSON.decode(jsonString);
+ _firstName = storedName['f'];
+ _appellation = storedName['a'];
+ }
+
+ static Future readyThePirates() {
+ var path = 'piratenames.json';
sethladd 2013/10/22 21:59:17 use String instead of var, since you use type anno
+ return HttpRequest.getString(path)
+ .then(_parsePirateNamesFromJSON);
+ }
+
+ static _parsePirateNamesFromJSON(String jsonString) {
+ Map pirateNames = JSON.decode(jsonString);
+ names = pirateNames['names'];
+ appellations = pirateNames['appellations'];
+ }
+}
« no previous file with comments | « src/diagrams/articles/trydart/textfieldscreenshot.png ('k') | src/site/articles/trydart/code/piratebadge.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698