| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #import('dart:html'); | 5 #import('dart:html'); |
| 6 #import('dart:json'); | 6 #import('dart:json'); |
| 7 | 7 |
| 8 /** | 8 /// Issue wraps JSON structure that describes a bug. |
| 9 * Issue wraps JSON structure that describes a bug. | |
| 10 */ | |
| 11 class Issue { | 9 class Issue { |
| 12 final json; | 10 final json; |
| 13 Issue(this.json); | 11 Issue(this.json); |
| 14 | 12 |
| 15 void addTo(Element div) { | 13 void addTo(Element div) { |
| 16 div.elements.add(new Element.tag("h2")..text = json[@"title"][@"$t"]); | 14 div.elements.add(new Element.tag("h2")..text = json[@"title"][@"$t"]); |
| 17 div.elements.add(new Element.tag("pre")..text = json[@"content"][@"$t"]); | 15 div.elements.add(new Element.tag("pre")..text = json[@"content"][@"$t"]); |
| 18 } | 16 } |
| 19 } | 17 } |
| 20 | 18 |
| 21 /** | 19 /// Decodes JSON into a list of Issues. |
| 22 * Decodes JSON into a list of Issues. | |
| 23 */ | |
| 24 List<Issue> getIssues(json) { | 20 List<Issue> getIssues(json) { |
| 25 var issues = json["feed"]["entry"]; | 21 var issues = json["feed"]["entry"]; |
| 26 if (issues == null) return null; | 22 if (issues == null) return null; |
| 27 return issues.map((data) => new Issue(data)); | 23 return issues.map((data) => new Issue(data)); |
| 28 } | 24 } |
| 29 | 25 |
| 30 /** | 26 /// Iterates over the recieved issues and construct HTML for them. |
| 31 * Iterates over the recieved issues and construct HTML for them. | |
| 32 */ | |
| 33 void processJson(json) { | 27 void processJson(json) { |
| 34 Element div = query("#content"); | 28 Element div = query("#content"); |
| 35 List<Issue> list = getIssues(json); | 29 List<Issue> list = getIssues(json); |
| 36 if (list == null) { | 30 if (list == null) { |
| 37 div.elements.add(new Element.tag("h2")..text = "... no issues found."); | 31 div.elements.add(new Element.tag("h2")..text = "... no issues found."); |
| 38 } else { | 32 } else { |
| 39 getIssues(json).forEach((Issue i) => i.addTo(div)); | 33 getIssues(json).forEach((Issue i) => i.addTo(div)); |
| 40 } | 34 } |
| 41 } | 35 } |
| 42 | 36 |
| 43 /** | 37 /// Sends a HTTPRequest and returns a future fo the date. |
| 44 * Sends a HTTPRequest and returns a future fo the date. | |
| 45 */ | |
| 46 Future<Dynamic> requestJson(String url) { | 38 Future<Dynamic> requestJson(String url) { |
| 47 Completer c = new Completer<Dynamic>(); | 39 Completer c = new Completer<Dynamic>(); |
| 48 void callback(HttpRequest req) { | 40 void callback(HttpRequest req) { |
| 49 if (req.readyState == HttpRequest.DONE) { | 41 if (req.readyState == HttpRequest.DONE) { |
| 50 c.complete(JSON.parse(req.response)); | 42 c.complete(JSON.parse(req.response)); |
| 51 } | 43 } |
| 52 }; | 44 }; |
| 53 new HttpRequest.get(url, callback); | 45 new HttpRequest.get(url, callback); |
| 54 return c.future; | 46 return c.future; |
| 55 } | 47 } |
| 56 | 48 |
| 57 void main() { | 49 void main() { |
| 58 // Requests new issues (can=new) from the Dart issue database. | 50 // Requests new issues from the Dart issue database as json. |
| 59 final String url = | 51 const String url = "https://code.google.com/feeds/issues/p/dart/issues/full"; |
| 60 "https://code.google.com/feeds/issues/p/dart/issues/full?alt=json&can=new"; | 52 const String options = "alt=json&can=new"; |
| 61 requestJson(url).then(processJson); | 53 requestJson("$url?$options").then(processJson); |
| 62 } | 54 } |
| OLD | NEW |