Skip to content

Commit

Permalink
- Fixes status_type definition.
Browse files Browse the repository at this point in the history
- Adds serialisation with basis on the MIME type.
  • Loading branch information
robotlolita committed Apr 15, 2012
1 parent a5ee904 commit 36ff6db
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/http.js
Expand Up @@ -69,9 +69,13 @@ function object_p(subject) {
return class_of(subject) == '[object Object]' } return class_of(subject) == '[object Object]' }


function status_type(status) { function status_type(status) {
var type = (status - 1).toString().charAt(0) var type = status.toString().charAt(0) - 1
return statuses[type] } return statuses[type] }


function serialise_for_type(mime, data) {
return mime == 'application/json'? JSON.stringify(data)
: /* otherwise */ serialise(data) }



//// -- Public interface ------------------------------------------------------ //// -- Public interface ------------------------------------------------------
var active = [] var active = []
Expand Down Expand Up @@ -138,21 +142,29 @@ var PromiseP = Promise.derive({




function request(uri, options) { function request(uri, options) {
var client, promise, method var client, promise, method, serialise_body_p, mime
options = options || {} options = options || {}
method = (options.method || 'GET').toUpperCase() options.headers = options.headers || {}
uri = build_uri(uri, options.query, options.body) method = (options.method || 'GET').toUpperCase()
uri = build_uri(uri, options.query, options.body)

options.headers['X-Requested-With'] = 'XMLHttpRequest'

serialise_body_p = object_p(options.body)
if (serialise_body_p) {
mime = options.headers['Content-Type'] || 'application/x-www-form-urlencoded'
options.body = serialise_for_type(mime, options.body)
options.headers['Content-Type'] = mime }


client = make_xhr() client = make_xhr()
promise = PromiseP.make(client, uri, options) promise = PromiseP.make(client, uri, options)


setup_headers(options.headers || {})
setup_listeners() setup_listeners()


setTimeout(function() { setTimeout(function() {
client.open(method, uri, true, options.username, options.password) client.open(method, uri, true, options.username, options.password)
client.send( object_p(options.body)? serialise(options.body) setup_headers(options.headers || {})
: /* otherwise */ options.body )}) client.send(options.body) })


active.push(promise) active.push(promise)


Expand Down

0 comments on commit 36ff6db

Please sign in to comment.