Permalink
Browse files

- add serialize

- add serializeArray
- more tests
- new valentine dependency
- reinstall node modules
- bump package
  • Loading branch information...
ded committed May 10, 2011
1 parent da5e2a8 commit bf4b05044225d6cddc82aa2785651509528b3ece
Showing with 277 additions and 79 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .gitmodules
  3. +5 −1 Makefile
  4. +1 −1 Makefile.js
  5. +11 −11 README.md
  6. +1 −1 build/connect
  7. +1 −1 build/mime
  8. +1 −1 build/sink
  9. +1 −1 build/smoosh
  10. +1 −0 build/valentine
  11. +6 −4 make/tests.js
  12. +6 −3 package.json
  13. +81 −23 reqwest.js
  14. +6 −1 reqwest.min.js
  15. +5 −2 src/ender.js
  16. +81 −23 src/reqwest.js
  17. +1 −1 tests/fixtures/fixtures_jsonp.js
  18. +1 −1 tests/fixtures/fixtures_jsonp3.js
  19. +23 −0 tests/tests.html
  20. +41 −4 tests/tests.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -13,3 +13,6 @@
[submodule "build/connect"]
path = build/connect
url = git://github.com/senchalabs/connect.git
+[submodule "build/valentine"]
+ path = build/valentine
+ url = git://github.com/ded/valentine.git
View
@@ -1,4 +1,8 @@
boosh:
node Makefile.js
test:
- node make/tests.js
+ node make/tests.js
+install:
+ npm install connect
+ npm install mime
+ npm install dispatch
View
@@ -15,7 +15,7 @@ require('smoosh').config({
"evil": true,
"regexp": false,
"undef": false,
- "sub": false,
+ "sub": true,
"white": true,
"indent": 2,
"whitespace": true,
View
@@ -5,17 +5,18 @@ All over again.
The happs
---------
- git clone git://github.com/ded/Reqwest.git
- cd !$
- git submodule update --init
- make
+ $ git clone git://github.com/ded/reqwest.git
+ $ cd reqwest
+ $ git submodule update --init
+ $ make
+ $ make install
-The codes
+API
---------
``` js
reqwest('path/to/html', function (resp) {
- Q('#content').html(resp);
+ qwery('#content').html(resp);
});
```
@@ -25,7 +26,7 @@ reqwest({
type: 'json',
method: 'post',
success: function (resp) {
- Q('#content').html(resp.content);
+ qwery('#content').html(resp.content);
},
failure: function (err) { }
});
@@ -36,7 +37,7 @@ reqwest({
url: 'path/to/data.jsonp?callback=?',
type: 'jsonp',
success: function (resp) {
- Q('#content').html(resp.content);
+ qwery('#content').html(resp.content);
}
});
```
@@ -47,16 +48,15 @@ reqwest({
type: 'jsonp',
jsonpCallback: 'foo',
success: function (resp) {
- Q('#content').html(resp.content);
+ qwery('#content').html(resp.content);
}
});
```
The Tests
-----
make test
- open http://localhost:3000
-
+ open http://localhost:1234
Browser support
---------------
Submodule mime updated from da6262 to 8f2c60
Submodule valentine added at 3f0f42
View
@@ -1,17 +1,19 @@
var http = require('http'),
exec = require('child_process').exec,
fs = require('fs'),
- Connect = require('../build/connect/'),
- dispatch = require('../build/dispatch/'),
- mime = require('../build/mime/');
+ Connect = require('connect'),
+ dispatch = require('dispatch'),
+ mime = require('mime');
+
+
var routes = {
'/': function (req, res) {
res.write(fs.readFileSync('./tests/tests.html', 'utf8'));
res.end();
},
- '(([a-zA-Z0-9_\\-\\/]+)\\.(css|js|json|html)$)': function (req, res, m, whole, file, ext) {
+ '(([a-zA-Z0-9_\\-\\/]+)\\.(css|js|json|html)$)': function (req, res, m, file, ext) {
res.writeHead(200, {'Content-Type': mime.lookup(ext)});
var asset = fs.readFileSync('./' + file + '.' + ext);
res.write(asset);
View
@@ -1,13 +1,16 @@
{
"name": "reqwest",
"description": "A robust lightweight wrapper for asynchronous http requests",
- "version": "0.0.9",
+ "version": "0.1.0",
"homepage": "https://github.com/ded/reqwest",
- "authors": ["Dustin Diaz", "Jacob Thornton"],
+ "author": "Dustin Diaz <@ded>",
"repository": {
"type": "git",
"url": "https://github.com/ded/reqwest.git"
},
"main": "./reqwest.js",
- "ender": "./src/ender.js"
+ "ender": "./src/ender.js",
+ "dependencies": {
+ "valentine": ">= 1.1.0"
+ }
}
View
@@ -4,24 +4,27 @@
* https://github.com/ded/reqwest
* license MIT
*/
-!function (context) {
+/*!
+ * Reqwest! A x-browser general purpose XHR connection manager
+ * copyright Dustin Diaz 2011
+ * https://github.com/ded/reqwest
+ * license MIT
+ */
+!function (window) {
var twoHundo = /^20\d$/,
+ doc = document,
+ byTag = 'getElementsByTagName',
+ topScript = doc[byTag]('script')[0],
+ head = topScript.parentNode,
xhr = ('XMLHttpRequest' in window) ?
function () {
return new XMLHttpRequest();
} :
function () {
return new ActiveXObject('Microsoft.XMLHTTP');
};
-
- var uniqid = (function () {
- var id = 0;
- return {
- get: function () {
- return id++;
- }
- };
- }());
+
+ var uniqid = 0;
function readyState(o, success, error) {
return function () {
@@ -45,47 +48,47 @@
}
}
}
-
+
function getCallbackName(o) {
var callbackVar = o.jsonpCallback || "callback";
if (o.url.substr(-(callbackVar.length + 2)) == (callbackVar + "=?")) {
// Generate a guaranteed unique callback name
- var callbackName = "Request" + uniqid.get();
-
+ var callbackName = "reqwest_" + uniqid++;
+
// Replace the ? in the URL with the generated name
o.url = o.url.substr(0, o.url.length - 1) + callbackName;
return callbackName;
} else {
// Find the supplied callback name
- var regex = new RegExp(callbackVar + "=([A-Za-z0-9_]+)");
+ var regex = new RegExp(callbackVar + "=([\\w]+)");
return o.url.match(regex)[1];
}
}
function getRequest(o, fn, err) {
if (o.type == 'jsonp') {
- var script = document.createElement('script');
- var head = document.getElementsByTagName('head')[0];
-
+ var script = doc.createElement('script');
+
// Add the global callback
var callbackName = getCallbackName(o);
window[callbackName] = function (data) {
// Call the success callback
o.success && o.success(data);
};
-
+
// Setup our script element
script.type = "text/javascript";
script.src = o.url;
+ script.async = true;
script.onload = function () {
// Script has been loaded, and thus the user callback has
// been called, so lets clean up now.
head.removeChild(script);
delete window[callbackName];
};
-
+
// Add the script to the DOM head
- head.appendChild(script);
+ head.insertBefore(script, topScript);
} else {
var http = xhr();
http.open(o.method || 'GET', typeof o == 'string' ? o : o.url, true);
@@ -183,11 +186,66 @@
return new Reqwest(o, fn);
}
- var old = context.reqwest;
+ function enc(v) {
+ return encodeURIComponent(v);
+ }
+
+ function serial(el) {
+ var n = el.name;
+ // don't serialize elements that are disabled or without a name
+ if (el.disabled || !n) {
+ return '';
+ }
+ n = enc(n);
+ switch (el.tagName.toLowerCase()) {
+ case 'input':
+ switch (el.type) {
+ // silly wabbit
+ case 'reset':
+ case 'button':
+ case 'image':
+ case 'file':
+ return '';
+ case 'checkbox':
+ case 'radio':
+ return el.checked ? n + '=' + (el.value ? enc(el.value) : true) + '&' : '';
+ default: // text hidden password submit
+ return n + '=' + (el.value ? enc(el.value) : true) + '&';
+ }
+ break;
+ case 'textarea':
+ return n + '=' + enc(el.value) + '&';
+ case 'select':
+ // @todo refactor beyond basic single selected value case
+ return n + '=' + enc(el.options[el.selectedIndex].value) + '&';
+ }
+ return '';
+ }
+
+ reqwest.serialize = function (form) {
+ var inputs = form[byTag]('input'),
+ selects = form[byTag]('select'),
+ texts = form[byTag]('textarea');
+ return (v(inputs).chain().toArray().map(serial).value().join('') +
+ v(selects).chain().toArray().map(serial).value().join('') +
+ v(texts).chain().toArray().map(serial).value().join('')).replace(/&$/, '');
+ };
+
+ reqwest.serializeArray = function (f) {
+ for (var pairs = this.serialize(f).split('&'), i = 0, l = pairs.length, r = [], o; i < l; i++) {
+ pairs[i] && (o = pairs[i].split('=')) && r.push({name: o[0], value: o[1]});
+ }
+ return r;
+ };
+
+ var old = window.reqwest;
reqwest.noConflict = function () {
- context.reqwest = old;
+ window.reqwest = old;
return this;
};
- context.reqwest = reqwest;
+
+ // defined as extern for Closure Compilation
+ // do not change to (dot) '.' syntax
+ window['reqwest'] = reqwest;
}(this);
View

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

Oops, something went wrong.
View
@@ -1,3 +1,6 @@
-$.ender({
- ajax: reqwest.noConflict()
+ender.ender({
+ ajax: reqwest,
+ serialize: function () {
+ return reqwest.serialize(this[0]);
+ }
});
Oops, something went wrong.

0 comments on commit bf4b050

Please sign in to comment.