Guest User

d3.jsonp plugin worldbank

a guest
Oct 22nd, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // modified d3.jsonp plugin from https://github.com/d3/d3-plugins/blob/master/jsonp/jsonp.js
  2. // switch `callback` on lines 10 and 24 to `prefix`
  3. d3.jsonp = function (url, callback) {
  4.   function rand() {
  5.     var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
  6.       c = '', i = -1;
  7.     while (++i < 15) c += chars.charAt(Math.floor(Math.random() * 52));
  8.     return c;
  9.   }
  10.  
  11.   function create(url) {
  12.     var e = url.match(/prefix=d3.jsonp.(\w+)/),
  13.       c = e ? e[1] : rand();
  14.     d3.jsonp[c] = function(data) {
  15.       callback(data);
  16.       delete d3.jsonp[c];
  17.       script.remove();
  18.     };
  19.     return 'd3.jsonp.' + c;
  20.   }
  21.  
  22.   var cb = create(url),
  23.     script = d3.select('head')
  24.     .append('script')
  25.     .attr('type', 'text/javascript')
  26.     .attr('src', url.replace(/(\{|%7B)prefix(\}|%7D)/, cb));
  27. };
Add Comment
Please, Sign In to add comment