Permalink
Browse files

Versions 1.0

Added JSONP support for cross-domain requests.
  • Loading branch information...
1 parent e0bbc0a commit 2e3f63393d81ef9e93d20a1ed723611289525783 @kylebarrow committed Oct 7, 2012
Showing with 81 additions and 35 deletions.
  1. +10 −6 README.md
  2. +1 −1 chibi-min.js
  3. +70 −28 chibi.js
View
@@ -1,4 +1,4 @@
-# Chibi v0.9.2
+# Chibi v1.0
#### A tiny JavaScript micro-framework
@@ -397,13 +397,15 @@ If only the *html* argument is specified, this will replace the inner HTML of th
</html>
```
-#### $(selector).ajax(url,*method*,*callback*,*nocache*)
-*Sends an AJAX request, optionally firing a callback with the XHR `responseText` on success*
+#### $(selector).ajax(url,*method*,*callback*,*nocache*,*nojsonp*)
+*Sends an AJAX request, optionally firing a callback with the XHR `responseText` and `status`*
-**ajax** uses the GET method if none is specified. When *nocache* is true, a `_ts` time stamp is added to the URL to prevent caching, yes, I'm looking at you Android Browser.
+**ajax** uses the GET method if none is specified. When *nocache* is true, a `_ts` time stamp is added to the URL to prevent caching, yes, I'm looking at you Android Browser and iOS 6.
**ajax** supports JSON as a selector ({'name','value'}), useful for when you want to send data without using form field DOM elements.
+For cross-domain requests, **ajax** uses JSONP by default but this can be overridden if *nojsonp* is true. JSONP requests will apply any *callback* to `callback=?` or similar in the **ajax** url. The *method* is obviously always `GET` for JSONP request.
+
```html
<!DOCTYPE html>
<html>
@@ -416,8 +418,10 @@ If only the *html* argument is specified, this will replace the inner HTML of th
</form>
<script>
$('form').ajax('ajax.html'); // XHR all form data using "GET" to "ajax.html"
- $({'text':'Foo Bar'}).ajax('ajax.html'); // XHR the JSON using "GET" to "ajax.html"
- $('form').ajax('ajax.html',"POST",function(data){alert(data)},true); // XHR all form data using "POST" to "ajax.html", alerts results on success, adds a cache busting time stamp
+ $({text:'Foo Bar'}).ajax('ajax.html'); // XHR the JSON using "GET" to "ajax.html"
+ $('form').ajax('ajax.html',"POST",function(data,status){ },true); // XHR all form data using "POST" to "ajax.html", returns responseText and status, adds a cache busting time stamp
+ $({sort:'created',direction:'asc'}).ajax('https://api.github.com/users/kylebarrow/repos?callback=?','GET',function(data,status){ }); // JSONP
+
</script>
</body>
</html>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,4 +1,4 @@
-/*Chibi v0.9.2, Copyright (C) 2012 Kyle Barrow
+/*Chibi v1.0, Copyright (C) 2012 Kyle Barrow
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@@ -676,55 +676,97 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
}, nodes);
},
// Basic XHR 1, no file support. Shakes fist at IE
- ajax: function (url, method, callback, nocache) {
+ ajax: function (url, method, callback, nocache, nojsonp) {
var xhr,
query = serializeData(nodes),
querystart = (url.indexOf('?') === -1) ? '?' : '&',
- timestamp = '_ts=' + (+new Date());
+ hostsearch = new RegExp('http[s]?://(.*?)/', 'gi'),
+ domain = hostsearch.exec(url),
+ timestamp = '_ts=' + (+new Date()),
+ head = d.getElementsByTagName('head')[0],
+ jsonpcallback = 'Chibi' + (+new Date()),
+ script;
- method = method || 'GET';
- if (w.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- } else if (w.ActiveXObject) {
- xhr = new ActiveXObject('Microsoft.XMLHTTP'); // IE < 9
- }
+ // JSONP if cross domain url
+ if (!nojsonp && domain && w.location.host !== domain[1]) {
- if (xhr) {
- method = method.toUpperCase();
+ url += querystart + query;
- if (method === 'GET') {
- url += querystart + query;
+ if (nocache) {
+ url += '&' + timestamp;
}
- if (nocache) {
- url += (method === 'POST') ? querystart + timestamp : '&' + timestamp;
+ // Replace jsonp ? with callback
+ if (callback && url.indexOf('=?') !== -1) {
+
+ url = url.replace('=?', '=' + jsonpcallback);
+
+ w[jsonpcallback] = function (data) {
+ try {
+ callback(data, 200);
+ } catch (e) {}
+ };
}
- // Douglas Crockford: "Synchronous programming is disrespectful and should not be employed in applications which are used by people"
- xhr.open(method, url, true);
+ script = document.createElement('script');
+ script.async = 'async';
+ script.src = url;
+
+ script.onload = function () {
+ head.removeChild(script);
+ };
+
+ head.appendChild(script);
+
+ } else {
+
+ method = method || 'GET';
- if (method === 'POST') {
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ if (w.XMLHttpRequest) {
+ xhr = new XMLHttpRequest();
+ } else if (w.ActiveXObject) {
+ xhr = new ActiveXObject('Microsoft.XMLHTTP'); // IE < 9
}
- xhr.send(query);
+ if (xhr) {
+ method = method.toUpperCase();
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4 && xhr.status === 200) {
- if (callback) {
- try {
- callback(xhr.responseText);
- } catch (e) {}
+ if (method === 'GET') {
+ url += querystart + query;
+ query = null;
+ }
+
+ if (nocache) {
+ url += (method === 'POST') ? querystart + timestamp : '&' + timestamp;
+ }
+
+ // Douglas Crockford: "Synchronous programming is disrespectful and should not be employed in applications which are used by people"
+ xhr.open(method, url, true);
+
+ xhr.onreadystatechange = function () {
+ if (xhr.readyState === 4) {
+ if (callback) {
+ try {
+ callback(xhr.responseText, xhr.status);
+ } catch (e) {}
+ }
}
+ };
+
+ if (method === 'POST') {
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
- };
+
+ xhr.send(query);
+
+ }
}
}
};
}
- // Set Chibi's global namespace here
+ // Set Chibi's global namespace here ($)
w.$ = chibi;
}());

0 comments on commit 2e3f633

Please sign in to comment.