DartJSONP icon indicating copy to clipboard operation
DartJSONP copied to clipboard

Utility class to allow users to easily make use of JSONP callback data in Dart

DartJSONP

Last tested working (Dartium, JS) with build 8942

(Created at the London Dart hackathon)

Inspired by Seth's blog post which explained how to use JSONP and postmessage with Dart: http://blog.sethladd.com/2012/03/jsonp-with-dart.

Allow easy JSONP data access without the fuss by injecting and removing a javascript callback function or Future<Map> and a dart postmessage handler function

For example (with callback):

var callbackFuncName = "twitterFunction";
JsonpCallback twitterCallback = new JsonpCallback(callbackFuncName);
twitterCallback.onDataReceived = (Map data) {
  // do something with the returned data
};

var twitterUrl = "http://search.twitter.com/search.json?q=dartlang&callback=$callbackFuncName";
twitterCallback.doCallback(twitterUrl);

For example (with Future):

var callbackFuncName = "twitterFunction";
JsonpCallback twitterCallback = new JsonpCallback(callbackFuncName);

var twitterUrl = "http://search.twitter.com/search.json?q=dartlang&callback=$callbackFuncName";
twitterCallback.doCallback(twitterUrl).then((Map data) {
  // do something with the returned data
});

You can see this running here: http://example.dartwatch.com/jsonp/DartJsonPtest.html

Internally, it adds the javascript, performs the request and when the callback is received by javascript and forwarded back to dart, it removes the scripts (as best it can), to clean up after itself.