Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow async=false requests #42

Merged
merged 2 commits into from

2 participants

@ntoll
Owner

This branch introduces the following changes:

  • Ajax calls can now be made synchronously (as detailed below)
  • A test has been added to ensure this behaviour works as expected

Example usage:

  var fi = fluidinfo({username: "username, password: "password"});
  var options = {
    path: "namespaces/ntoll",
    args: {
      returnDescription: true,
      returnTags: true
    },
    async: false
  };
  var result = fi.api.get(options);

Note how the call to the api level function returns a result object.

I'VE NOT UPDATED THE HIGHER LEVEL UTILITY FUNCTIONS WITH THIS BEHAVIOUR

(Doing so would make the code more complex, convoluted and hard to follow only so some numpty JS programmer can make AJAX calls in the WRONG way).

@ntoll
Owner

Forgot to mention that this addresses issue #5

@terrycojones

I don't know enough about people using Javascript to know if having an async option is worth it (perhaps for server-side stuff?) but this code looks good to me.

@ntoll
Owner

People shouldn't use async=false but onigiri and eric seem to want it.

@ntoll ntoll merged commit bc0920d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 13, 2011
  1. @ntoll

    Added failing unit test

    ntoll authored
  2. @ntoll

    Fixed tests to behave properly and made sure they pass. We now allow …

    ntoll authored
    …non-asynchronous calls :-(
This page is out of date. Refresh to see the latest.
Showing with 62 additions and 19 deletions.
  1. +38 −19 fluidinfo.js
  2. +24 −0 spec/fluidinfoSpec.js
View
57 fluidinfo.js
@@ -297,6 +297,27 @@ var fluidinfo = function(options) {
}
/**
+ * Builds a simplified (nice to use) result object
+ * @param xhr {Object} the XmlHttpRequest instance from which to build the
+ * result.
+ */
+ function createNiceResult(xhr) {
+ // build a simple result object
+ var result = new Object();
+ result.status = xhr.status;
+ result.statusText = xhr.statusText;
+ result.headers = getHeaders(xhr);
+ result.rawData = xhr.responseText;
+ if(isJSONData(result.headers['Content-Type'])) {
+ result.data = JSON.parse(xhr.responseText);
+ } else {
+ result.data = xhr.responseText;
+ }
+ result.request = xhr;
+ return result;
+ }
+
+ /**
* Sends an appropriate XMLHTTPRequest based request to Fluidinfo.
* @param options {Object} An object containing the following named options:
* <dl>
@@ -325,9 +346,14 @@ var fluidinfo = function(options) {
var method = options.type.toUpperCase() || "GET";
var args = createArgs(options.args);
var url = session.baseURL+options.path+args;
- var async = options.async || true;
+ var async = true;
+ if (options.async !== undefined) {
+ async = options.async;
+ }
var xhr = createXMLHTTPObject();
- if(!xhr) return;
+ if(!xhr) {
+ return;
+ }
xhr.open(method, url, async);
if(authorizationToken != ""){
xhr.setRequestHeader("Authorization", "Basic " + authorizationToken);
@@ -341,18 +367,7 @@ var fluidinfo = function(options) {
}
xhr.onreadystatechange = function() {
if(xhr.readyState != 4) return;
- // build a simple result object
- var result = new Object();
- result.status = xhr.status;
- result.statusText = xhr.statusText;
- result.headers = getHeaders(xhr);
- result.rawData = xhr.responseText;
- if(isJSONData(result.headers['Content-Type'])) {
- result.data = JSON.parse(xhr.responseText);
- } else {
- result.data = xhr.responseText;
- }
- result.request = xhr;
+ var result = createNiceResult(xhr);
// call the event handlers
if(xhr.status < 300 || xhr.status == 304) {
if(options.onSuccess){
@@ -364,6 +379,10 @@ var fluidinfo = function(options) {
}
}
xhr.send(options.data)
+ if(!async) {
+ var result = createNiceResult(xhr);
+ return result;
+ }
}
/**
@@ -378,7 +397,7 @@ var fluidinfo = function(options) {
api.get = function(options){
options.type = "GET";
options.data = null;
- sendRequest(options);
+ return sendRequest(options);
}
/**
@@ -387,7 +406,7 @@ var fluidinfo = function(options) {
*/
api.post = function(options){
options.type = "POST";
- sendRequest(options);
+ return sendRequest(options);
}
/**
@@ -396,7 +415,7 @@ var fluidinfo = function(options) {
*/
api.put = function(options){
options.type = "PUT";
- sendRequest(options);
+ return sendRequest(options);
}
/**
@@ -406,7 +425,7 @@ var fluidinfo = function(options) {
api.delete = function(options){
options.type = "DELETE";
options.data = null;
- sendRequest(options);
+ return sendRequest(options);
}
/**
@@ -416,7 +435,7 @@ var fluidinfo = function(options) {
api.head = function(options){
options.type = "HEAD";
options.data = null;
- sendRequest(options);
+ return sendRequest(options);
}
session.api = api;
View
24 spec/fluidinfoSpec.js
@@ -335,6 +335,30 @@ describe("Fluidinfo.js", function() {
expect(spy.calledOnce).toBeTruthy();
});
+ it("should return a simple response object for onSuccess when async=False", function() {
+ var options = new Object();
+ options.path = "namespaces/test";
+ var payload = {name: "foo", description: "bar"};
+ options.data = payload;
+ options.async = false;
+ var responseStatus = 201;
+ var responseHeaders = {"Content-Type": "application/json",
+ "Location": "http://fluiddb.fluidinfo.com/namespaces/test/foo",
+ "Content-Length": 107,
+ "Date": "Mon, 02 Aug 2010 12:40:41 GMT"}
+ var responseText = '{"id": "e9c97fa8-05ed-4905-9f72-8d00b7390f9b", "URI": "http://fluiddb.fluidinfo.com/namespaces/test/foo"}';
+ this.server.respondWith([responseStatus, responseHeaders, responseText]);
+ var result = this.fi.api.post(options);
+ expect(typeof(result)).toEqual("object");
+ expect(result.status).toEqual(201);
+ expect(result.statusText).toEqual("Created");
+ expect(typeof(result.headers)).toEqual("object");
+ expect(result.headers["Content-Type"]).toEqual("application/json");
+ expect(result.data).toBeTruthy();
+ expect(typeof(result.rawData)).toEqual("string");
+ expect(typeof(result.request)).toEqual("object"); // original XHR
+ });
+
it("should serialise Javascript objects into JSON", function() {
var options = new Object();
options.path = "namespaces/test";
Something went wrong with that request. Please try again.