| 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 // A JS interop sample showing JSONP access to Twitter from Dart. | |
| 6 | |
| 7 import 'dart:html'; | |
| 8 import 'package:js/js.dart' as js; | |
| 9 | |
| 10 void main() { | |
| 11 // Create a JavaScript function called display that forwards to the Dart | |
| 12 // function. | |
| 13 js.context.display = new js.Callback.once(display); | |
| 14 | |
| 15 // Inject a JSONP request to Twitter invoking the JavaScript display | |
| 16 // function. | |
| 17 document.body.nodes.add(new ScriptElement()..src = | |
| 18 "https://search.twitter.com/search.json?q=dartlang&rpp=20&callback=display")
; | |
| 19 } | |
| 20 | |
| 21 // Convert URLs in the text to links. | |
| 22 String linkify(String text) { | |
| 23 List words = text.split(' '); | |
| 24 var buffer = new StringBuffer(); | |
| 25 for (var word in words) { | |
| 26 if (!buffer.isEmpty) buffer.write(' '); | |
| 27 if (word.startsWith('http://') || word.startsWith('https://')) { | |
| 28 buffer.write('<a href="$word">$word</a>'); | |
| 29 } else { | |
| 30 buffer.write(word); | |
| 31 } | |
| 32 } | |
| 33 return buffer.toString(); | |
| 34 } | |
| 35 | |
| 36 // Display the JSON data on the web page. | |
| 37 // Note callbacks are automatically executed within a scope. | |
| 38 void display(var data) { | |
| 39 // The data and results objects are proxies to JavaScript object. | |
| 40 var results = data.results; | |
| 41 int length = results.length; | |
| 42 | |
| 43 for (int i = 0; i < length; ++i) { | |
| 44 var result = results[i]; | |
| 45 String user = result.from_user_name; | |
| 46 String text = linkify(result.text); | |
| 47 | |
| 48 var div = new DivElement() | |
| 49 ..innerHtml = '<div>From: $user</div><div>$text</div><p>'; | |
| 50 document.body.nodes.add(div); | |
| 51 } | |
| 52 } | |
| OLD | NEW |