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:async'; | 5 import 'dart:async'; |
6 import 'dart:html'; | 6 import 'dart:html'; |
7 import 'dart:json' as jsonlib; | 7 import "dart:convert"; |
8 | 8 |
9 /// Issue wraps JSON structure that describes a bug. | 9 /// Issue wraps JSON structure that describes a bug. |
10 class Issue { | 10 class Issue { |
11 final json; | 11 final json; |
12 Issue(this.json); | 12 Issue(this.json); |
13 | 13 |
14 void addTo(Element div) { | 14 void addTo(Element div) { |
15 div.children.add(new Element.tag("h2")..text = json[r"title"][r"$t"]); | 15 div.children.add(new Element.tag("h2")..text = json[r"title"][r"$t"]); |
16 div.children.add(new Element.tag("p")..text = json[r"content"][r"$t"]); | 16 div.children.add(new Element.tag("p")..text = json[r"content"][r"$t"]); |
17 } | 17 } |
18 } | 18 } |
19 | 19 |
20 /// Decodes JSON into a list of Issues. | 20 /// Decodes JSON into a list of Issues. |
21 List<Issue> getIssues(json) { | 21 List<Issue> getIssues(json) { |
22 var issues = json["feed"]["entry"]; | 22 var issues = json["feed"]["entry"]; |
23 if (issues == null) return null; | 23 if (issues == null) return null; |
24 return issues.map((data) => new Issue(data)).toList(); | 24 return issues.map((data) => new Issue(data)).toList(); |
25 } | 25 } |
26 | 26 |
27 /// Iterates over the received issues and construct HTML for them. | 27 /// Iterates over the received issues and construct HTML for them. |
28 void processJson(String jsonText) { | 28 void processJson(String jsonText) { |
29 var json = jsonlib.parse(jsonText); | 29 var json = JSON.decode(jsonText); |
30 Element div = query("#content"); | 30 Element div = query("#content"); |
31 List<Issue> list = getIssues(json); | 31 List<Issue> list = getIssues(json); |
32 if (list == null) { | 32 if (list == null) { |
33 div.children.add(new Element.tag("h2")..text = "... no issues found."); | 33 div.children.add(new Element.tag("h2")..text = "... no issues found."); |
34 } else { | 34 } else { |
35 list.forEach((Issue i) => i.addTo(div)); | 35 list.forEach((Issue i) => i.addTo(div)); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 /// Sends a HTTPRequest and returns a future for the data. | 39 /// Sends a HTTPRequest and returns a future for the data. |
40 Future<dynamic> requestJson(String url) { | 40 Future<dynamic> requestJson(String url) { |
41 Completer c = new Completer<dynamic>(); | 41 Completer c = new Completer<dynamic>(); |
42 void callback(HttpRequest req) { | 42 void callback(HttpRequest req) { |
43 if (req.readyState == HttpRequest.DONE) { | 43 if (req.readyState == HttpRequest.DONE) { |
44 c.complete(jsonlib.parse(req.response)); | 44 c.complete(JSON.decode(req.response)); |
45 } | 45 } |
46 }; | 46 }; |
47 return HttpRequest.getString(url); | 47 return HttpRequest.getString(url); |
48 } | 48 } |
49 | 49 |
50 void main() { | 50 void main() { |
51 // Requests new issues from the Dart issue database as json. | 51 // Requests new issues from the Dart issue database as json. |
52 const String url = "https://code.google.com/feeds/issues/p/dart/issues/full"; | 52 const String url = "https://code.google.com/feeds/issues/p/dart/issues/full"; |
53 const String options = "alt=json&can=new"; | 53 const String options = "alt=json&can=new"; |
54 requestJson("$url?$options").then(processJson); | 54 requestJson("$url?$options").then(processJson); |
55 } | 55 } |
OLD | NEW |