Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor to use instances over singletons, added minified version

Changed the interface so you create a new TinyP object for each JSONP
call you want to make
  • Loading branch information...
commit f1339c1a2013eb7831960f86e447b05af858ed75 1 parent fd6c589
Mike Singleton authored

Showing 2 changed files with 51 additions and 73 deletions. Show diff stats Hide diff stats

  1. +2 0  tinyp-min.js
  2. +49 73 tinyp.js
2  tinyp-min.js
... ... @@ -0,0 +1,2 @@
  1 +function TinyP(a,b){this.url=a;this.callback=b;this.internalCallback=this.generateCallback();var c=this.generateScript();c.setAttribute("src",a+"&callback=TinyP."+this.internalCallback);this.script=document.getElementsByTagName("head")[0].appendChild(c)}
  2 +TinyP.prototype={generateRandName:function(){for(var a="",b=0;b<15;b++)a+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_".charAt(Math.floor(Math.random()*54));return a},generateScript:function(){var a=document.createElement("script");a.setAttribute("type","text/javascript");return a},generateCallback:function(){var a=this.generateRandName(),b=this;TinyP[a]=function(c){b.callback(c);delete TinyP[a];b.script.parentNode.removeChild(b.script)};return a}};
122 tinyp.js
... ... @@ -1,88 +1,64 @@
1 1 /**
2 2 * A Simple (tiny) JSONP library
3 3 *
4   - * Usage: TinyP.load(url, callbackFunction);
  4 + * Usage: new TinyP(url, callbackFunction);
5 5 *
6 6 * @author Mike Singleton
7 7 *
8 8 * Todos:
9   - * - Don't execute until all JS is loaded
10 9 * - Have url include ? to indicate where to place the callback function (like jQuery)
11 10 */
12 11
13   -// Make JSLint happy
14   -/*global window,document */
  12 +/**
  13 + * TinyP constructor
  14 + * @constructor
  15 + * @param {string} url to call
  16 + * @param {function()} callback function to be executed after the data is fetched
  17 + */
  18 +function TinyP(url, callback) {
  19 + this.url = url;
  20 + this.callback = callback;
  21 + this.internalCallback = this.generateCallback();
  22 +
  23 + // Drop the script on the page
  24 + var script = this.generateScript();
  25 + script.setAttribute('src', url + '&callback=TinyP.' + this.internalCallback);
  26 + this.script = document.getElementsByTagName('head')[0].appendChild(script);
  27 +}
15 28
16   -(function() {
17   - var TinyP = {
18   - head: document.getElementsByTagName('head')[0],
19   - callbacks: {},
20   - scriptTags: {},
21   -
22   - /* Generate a random function name */
23   - generateRandName: function() {
24   - var name = '';
25   - var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_';
26   -
27   - for(var i = 0; i < 15; i++) {
28   - name += possible.charAt(Math.floor(Math.random() * possible.length));
29   - }
30   -
31   - return name;
32   - },
33   -
34   - /* Generate a script tag */
35   - generateScript: function() {
36   - var script = document.createElement('script');
37   - script.setAttribute('type', 'text/javascript');
38   - return script;
39   - },
40   -
41   - /* Add the script to the end of the head */
42   - addScript: function(script, name) {
43   - var tag = this.head.appendChild(script);
44   - this.scriptTags[name] = tag;
45   - },
46   -
47   - /* Generate a random callback function to pass as the JSONP callback */
48   - generateCallback: function() {
49   - var script = this.generateScript();
50   - var random = this.generateRandName();
51   -
52   - script.innerHTML = "function " + random + "(data) { TinyP.execute('" + random + "', data); }";
53   - this.addScript(script, random);
54   -
55   - return random;
56   - },
  29 +TinyP.prototype = {
  30 + /* Generate a random function name */
  31 + generateRandName: function() {
  32 + var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_';
  33 + var name = '';
  34 +
  35 + for(var i = 0; i < 15; i++) {
  36 + name += possible.charAt(Math.floor(Math.random() * possible.length));
  37 + }
  38 +
  39 + return name;
  40 + },
  41 +
  42 + /* Generate a script tag */
  43 + generateScript: function() {
  44 + var script = document.createElement('script');
  45 + script.setAttribute('type', 'text/javascript');
  46 + return script;
  47 + },
  48 +
  49 + /* Generate a random callback function to pass as the JSONP callback */
  50 + generateCallback: function() {
  51 + var random = this.generateRandName();
  52 + var self = this;
57 53
58   - /*
59   - * Execute the callback associated with the random callback function and
60   - * clean up all of the inserted JS afterwards
61   - */
62   - execute: function(callbackName, data) {
63   - this.callbacks[callbackName](data);
64   -
  54 + TinyP[random] = function(data) {
  55 + self.callback(data);
  56 +
65 57 // Cleanup
66   - delete this.callbacks[callbackName];
67   - var script = this.scriptTags[callbackName];
68   - var urlScript = this.scriptTags['_url_' + callbackName];
69   - script.parentNode.removeChild(script);
70   - urlScript.parentNode.removeChild(urlScript);
71   - },
72   -
73   - /*
74   - * Take a url and callback function and generate a random JSONP callback
75   - * and inject the script into the head
76   - */
77   - load: function(url, callback) {
78   - var callbackName = this.generateCallback();
79   - this.callbacks[callbackName] = callback;
80   -
81   - var script = this.generateScript();
82   - script.setAttribute('src', url + '&callback=' + callbackName);
83   - this.addScript(script, "_url_" + callbackName);
  58 + delete TinyP[random];
  59 + self.script.parentNode.removeChild(self.script);
84 60 }
85   - };
86 61
87   - window.TinyP = TinyP;
88   -})();
  62 + return random;
  63 + }
  64 +}

0 comments on commit f1339c1

Please sign in to comment.
Something went wrong with that request. Please try again.