Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix #120 replace jsonp example

  • Loading branch information...
commit 1b68ecf4cd62420773d18777c2d76618a4cb65d0 1 parent cd7ee93
@a14n a14n authored
View
48 example/jsonp/jsonp.dart
@@ -0,0 +1,48 @@
+library jsonp_sample;
+
+import 'dart:html';
+
+import 'package:js/js.dart';
+
+const List<String> FIELDS = const ['name', 'description', 'size',
+ 'watchers', 'forks'];
+
+TableElement table = querySelector('#repo-table');
+
+addTableHeadRow() {
+ var tr = new TableRowElement();
+ for (var field in FIELDS) {
+ tr.append(new Element.tag('th')..text = field);
+ }
+ table.querySelector('thead').append(tr);
+}
+
+addTableBodyRow(Proxy repo) {
+ var tr = new TableRowElement();
+ for (var field in FIELDS) {
+ var td = new TableCellElement();
+ if (field == 'name') {
+ td.append(new AnchorElement()
+ ..href = repo.html_url
+ ..text = repo[field]);
+ } else {
+ td.text = repo[field].toString();
+ }
+ tr.append(td);
+ }
+ table.querySelector('tbody').append(tr);
+}
+
+void main() {
+ // Create a jsObject to handle the response.
+ context.processData = (response) {
+ addTableHeadRow();
+ for (var i = 0; i < response.data.length; i++) {
+ addTableBodyRow(response.data[i]);
+ }
+ };
+
+ ScriptElement script = new Element.tag("script");
+ script.src = "https://api.github.com/users/dart-lang/repos?callback=processData";
+ document.body.children.add(script);
+}
View
22 example/jsonp/jsonp.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
+ rel="stylesheet">
+ <title>Using JSONP with Dart</title>
+</head>
+<body>
+ <div class="container">
+ <h1>Dart Repos on Github</h1>
+
+ <table id="repo-table" class="table table-bordered table-striped">
+ <thead></thead>
+ <tbody></tbody>
+ </table>
+ </div>
+ <script type="application/dart" src="jsonp.dart"></script>
+ <script src="packages/browser/dart.js"></script>
+ <script src="packages/browser/interop.js"></script>
+</body>
+</html>
View
52 example/twitter/twitter.dart
@@ -1,52 +0,0 @@
-// 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.
-
-// A JS interop sample showing JSONP access to Twitter from Dart.
-
-import 'dart:html';
-import 'package:js/js.dart' as js;
-
-void main() {
- // Create a JavaScript function called display that forwards to the Dart
- // function.
- js.context.display = display;
-
- // Inject a JSONP request to Twitter invoking the JavaScript display
- // function.
- document.body.nodes.add(new ScriptElement()..src =
- "https://search.twitter.com/search.json?q=dartlang&rpp=20&callback=display");
-}
-
-// Convert URLs in the text to links.
-String linkify(String text) {
- List words = text.split(' ');
- var buffer = new StringBuffer();
- for (var word in words) {
- if (!buffer.isEmpty) buffer.write(' ');
- if (word.startsWith('http://') || word.startsWith('https://')) {
- buffer.write('<a href="$word">$word</a>');
- } else {
- buffer.write(word);
- }
- }
- return buffer.toString();
-}
-
-// Display the JSON data on the web page.
-// Note callbacks are automatically executed within a scope.
-void display(var data) {
- // The data and results objects are proxies to JavaScript object.
- var results = data.results;
- int length = results.length;
-
- for (int i = 0; i < length; ++i) {
- var result = results[i];
- String user = result.from_user_name;
- String text = linkify(result.text);
-
- var div = new DivElement()
- ..innerHtml = '<div>From: $user</div><div>$text</div><p>';
- document.body.nodes.add(div);
- }
-}
View
14 example/twitter/twitter.html
@@ -1,14 +0,0 @@
-<!-- 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. -->
-
-<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
-<html> <head>
-<title></title>
-</head>
-
-<body>
-<script type="application/dart" src="twitter.dart"></script>
-<script src="packages/browser/dart.js"></script>
-<script src="packages/browser/interop.js"></script>
-</body> </html>
View
8 gh-pages-template/example/index.html
@@ -90,11 +90,11 @@
class="info"><a href="https://github.com/dart-lang/js-interop/blob/master/example/google-maps/directions.dart">directions.dart</a></td></tr></table></div>
<div class="app"><table><tr><td rowspan=2><a
- href="twitter/twitter.html"><img
+ href="jsonp/jsonp.html"><img
src="dart_32_32.gif" width=32 height=32></a></td><td
- class="title">Twitter Search via JSONP - <a
- href="twitter/twitter.html">twitter.html</a></td</tr><tr><td
- class="info"><a href="https://github.com/dart-lang/js-interop/blob/master/example/twitter/twitter.dart">twitter.dart</a></td></tr></table></div>
+ class="title">Github Search via JSONP - <a
+ href="jsonp/jsonp.html">jsonp.html</a></td</tr><tr><td
+ class="info"><a href="https://github.com/dart-lang/js-interop/blob/master/example/jsonp/jsonp.dart">jsonp.dart</a></td></tr></table></div>
<p class="footer"><a href="https://github.com/dart-lang/js-interop">https://github.com/dart-lang/js-interop</a></p>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.