-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6 remove jquery reliance #15
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1121400
Removed dependency on jQuery from the library. Hid the utility functi…
ntoll 1b179cb
Ensured that the onSuccess function is called.
ntoll d462d55
Moved a comment to the right block of code.
ntoll f3d88b3
Updates in light of review with Terry
ntoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,17 @@ | |
* @author <a href="http://twitter.com/onigiri">onigiri</a>, <a href="http://twitter.com/ntoll">ntoll</a> & <a href="http://twitter.com/barshirtcliff">barshirtcliff</a> | ||
* @version 0.1 | ||
* @constructor | ||
* @param options {Object} Contains various parameters for the call. | ||
* @param options {Object} Contains various parameters for the new session with | ||
* Fluidinfo. | ||
* <dl> | ||
* <dt>username</dt> | ||
* <dd>The username to use when authenticating with Fluidinfo.</dd> | ||
* <dt>password</dt> | ||
* <dd>The password to use when authenticating with Fluidinfo</dd> | ||
* <dt>instance</dt> | ||
* <dd>The instance to connect to. Either "main", "sandbox" or a bespoke | ||
* instance. Defaults to "main".</dd> | ||
* </dl> | ||
* returns {Object} An object through which one interacts with Fluidinfo. | ||
*/ | ||
fluidinfo = function(options) { | ||
|
@@ -63,8 +73,10 @@ fluidinfo = function(options) { | |
} | ||
} | ||
|
||
// The object to eventually be returned | ||
session = new Object(); | ||
/** | ||
* Represents a session with Fluidinfo. | ||
*/ | ||
var session = new Object(); | ||
|
||
if(options) { | ||
if(options.instance) { | ||
|
@@ -88,7 +100,6 @@ fluidinfo = function(options) { | |
} | ||
} | ||
} | ||
|
||
if((options.username != undefined) && (options.password != undefined)) { | ||
session.authorizationToken = Base64.encode(options.username + ":" + options.password); | ||
} | ||
|
@@ -99,16 +110,14 @@ fluidinfo = function(options) { | |
session.baseURL = "https://fluiddb.fluidinfo.com/"; | ||
} | ||
|
||
var utils = new Object(); | ||
|
||
/** | ||
* Magic voodoo used to identify an array (potentially a Fluidinfo set). | ||
* Taken from page 61 of Doug Crockford's "Javascript: The Good Parts". | ||
* | ||
* @param value A value that might be an array. | ||
* @return {boolean} An indication if the value is an array. | ||
*/ | ||
utils.isArray = function(value) { | ||
function isArray(value) { | ||
return Object.prototype.toString.apply(value) === "[object Array]"; | ||
}; | ||
|
||
|
@@ -120,7 +129,7 @@ fluidinfo = function(options) { | |
* '/' values. | ||
* @return {string} The appropriately encoded URL. | ||
*/ | ||
utils.encodeURL = function(path) { | ||
function encodeURL(path) { | ||
var result = ""; | ||
for(i=0; i<path.length; i++) { | ||
result += "/" + encodeURIComponent(path[i]); | ||
|
@@ -137,7 +146,7 @@ fluidinfo = function(options) { | |
* @param value A value whose "type" needs identifying | ||
* @return A boolean indication if the value is a Fluidinfo primitive type. | ||
*/ | ||
utils.isPrimitive = function(value) { | ||
function isPrimitive(value) { | ||
// check the easy type matches first | ||
var valueType = typeof(value); | ||
var primitiveTypes = ["number", "string", "boolean"]; | ||
|
@@ -152,7 +161,7 @@ fluidinfo = function(options) { | |
} | ||
// check for an array (potential set) and validate it only contains | ||
// strings (currently multi-type arrays are not allowed) | ||
if(utils.isArray(value)) { | ||
if(isArray(value)) { | ||
for(i=0; i<value.length; i++) { | ||
memberType = typeof(value[i]); | ||
if(memberType !== "string") { | ||
|
@@ -174,7 +183,7 @@ fluidinfo = function(options) { | |
* @return the most appropriate MIME to use of null if not appropriate / | ||
* required. | ||
*/ | ||
utils.detectContentType = function(options) { | ||
function detectContentType(options) { | ||
// a "PUT" to objects/ or about/ endpoints means dealing with the MIME | ||
// of a tag-value. If no MIME type is passed in the options objects | ||
// then check if the value is a Fluidinfo primitive type. Otherwise | ||
|
@@ -183,7 +192,7 @@ fluidinfo = function(options) { | |
if(options.type==="PUT" && (options.url.match("^objects\/") || options.url.match("^about\/"))) { | ||
if(options.contentType){ | ||
result = options.contentType; | ||
} else if (api.utils.isPrimitive(options.data)) { | ||
} else if (isPrimitive(options.data)) { | ||
result = "application/vnd.fluiddb.value+json"; | ||
} else { | ||
throw { name: "ValueError", message: "Must supply Content-Type"}; | ||
|
@@ -195,43 +204,97 @@ fluidinfo = function(options) { | |
return result; | ||
} | ||
|
||
var api = new Object(); | ||
|
||
api.utils = utils; | ||
/** | ||
* Returns an appropriate XMLHttpRequest Object depending on the browser. | ||
* Based upon code from here: | ||
* http://www.quirksmode.org/js/xmlhttp.html | ||
*/ | ||
function createXMLHTTPObject() { | ||
var XMLHttpFactories = [ | ||
function () {return new XMLHttpRequest()}, | ||
function () {return new ActiveXObject("Msxml2.XMLHTTP")}, | ||
function () {return new ActiveXObject("Msxml3.XMLHTTP")}, | ||
function () {return new ActiveXObject("Microsoft.XMLHTTP")} | ||
]; | ||
var xhr = false; | ||
for(var i=0; i<XMLHttpFactories.length; i++) { | ||
try { | ||
xhr = XMLHttpFactories[i](); | ||
} catch(e) { | ||
continue; | ||
} | ||
break; | ||
} | ||
return xhr; | ||
} | ||
|
||
/** | ||
* Makes an appropriate AJAX request to Fluidinfo | ||
* Sends an appropriate XMLHTTPRequest based request to Fluidinfo. | ||
* @param options {Object} An object containing the following named options: | ||
* <dl> | ||
* <dt>type</dt> | ||
* <dd>The request's HTTP method. [GET, POST, PUT, DELETE or HEAD]</dd> | ||
* <dt>url</dt> | ||
* <dd>The full URL of the resource to which the request is made.</dd> | ||
* <dt>data</dt> | ||
* <dd>The body of data that may be the payload of the request (in the | ||
* case of POST and PUT requests).</dd> | ||
* <dt>async</dt> | ||
* <dd>Indicates if the request is to be asyncronous (default is true)</dd> | ||
* <dt>onSuccess</dt> | ||
* <dd>A function that takes the XHR request as an argument. Called upon | ||
* successful completion of the request.</dd> | ||
* <dt>onError</dt> | ||
* <dd>A function that takes the XHR request as an argument. Called when | ||
* the request resulted in an error.</dd> | ||
* </dl> | ||
* | ||
* @private | ||
* @param options {Object} Contains various parameters for the call. | ||
*/ | ||
api.ajax = function(options){ | ||
options.contentType = api.utils.detectContentType(options); | ||
if(api.utils.isArray(options.url)) { | ||
options.url = api.utils.encodeURL(options.url); | ||
function sendRequest(options) { | ||
if(isArray(options.url)) { | ||
options.url = encodeURL(options.url); | ||
} | ||
options.url = session.baseURL+options.url; | ||
options.async = options.async || true; | ||
options.beforeSend = function(xhrObj){ | ||
if(session.authorizationToken != undefined){ | ||
xhrObj.setRequestHeader("Authorization","Basic " + session.authorizationToken); | ||
}; | ||
if(options.contentType) { | ||
xhrObj.setRequestHeader("Content-Type", options.contentType); | ||
} | ||
var method = options.type.toUpperCase() || "GET"; | ||
var url = session.baseURL+options.url; | ||
var async = options.async || true; | ||
var xhr = createXMLHTTPObject(); | ||
if(!xhr) return; | ||
xhr.open(method, url, async); | ||
if(session.authorizationToken != undefined){ | ||
xhr.setRequestHeader("Authorization", "Basic " + session.authorizationToken); | ||
}; | ||
options.processData = false; | ||
$.ajax(options); | ||
var contentType = detectContentType(options); | ||
if(contentType) { | ||
xhr.setRequestHeader("Content-Type", contentType); | ||
} | ||
xhr.onreadystatechange = function() { | ||
if(xhr.readyState != 4) return; | ||
if(xhr.status < 300 && xhr.status != 304) { | ||
if(options.onSuccess){ | ||
options.onSuccess(xhr); | ||
} | ||
} else if (options.onError){ | ||
// there appears to be a problem | ||
options.onError(xhr); | ||
} | ||
} | ||
if(xhr.readyState == 4) return xhr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this..? |
||
xhr.send(options.data) | ||
} | ||
|
||
/** | ||
* Contains functions to facilitate the calling of the Fluidinfo REST API. | ||
*/ | ||
var api = new Object(); | ||
|
||
/** | ||
* Makes an HTTP GET call to the Fluidinfo API | ||
* | ||
*/ | ||
api.get = function(options){ | ||
options.type = "GET"; | ||
options.data = null; | ||
session.api.ajax(options); | ||
sendRequest(options); | ||
} | ||
|
||
/** | ||
|
@@ -240,7 +303,7 @@ fluidinfo = function(options) { | |
*/ | ||
api.post = function(options){ | ||
options.type = "POST"; | ||
session.api.ajax(options); | ||
sendRequest(options); | ||
} | ||
|
||
/** | ||
|
@@ -249,7 +312,7 @@ fluidinfo = function(options) { | |
*/ | ||
api.put = function(options){ | ||
options.type = "PUT"; | ||
session.api.ajax(options); | ||
sendRequest(options); | ||
} | ||
|
||
/** | ||
|
@@ -259,7 +322,7 @@ fluidinfo = function(options) { | |
api.delete = function(options){ | ||
options.type = "DELETE"; | ||
options.data = null; | ||
session.api.ajax(options); | ||
sendRequest(options); | ||
} | ||
|
||
/** | ||
|
@@ -269,7 +332,7 @@ fluidinfo = function(options) { | |
api.head = function(options){ | ||
options.type = "HEAD"; | ||
options.data = null; | ||
session.api.ajax(options); | ||
sendRequest(options); | ||
} | ||
|
||
session.api = api; | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix this to xhr.status < 300 || xhr.status == 304