2013-10-29 07:19:27 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-02-28 14:14:32 -05:00
|
|
|
var config = require('js/app-config').config;
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
var RestDAO = function(baseUri) {
|
|
|
|
if (baseUri) {
|
|
|
|
this._baseUri = baseUri;
|
2013-10-29 07:19:27 -04:00
|
|
|
} else {
|
|
|
|
this._baseUri = config.cloudUrl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET (read) request
|
2013-11-08 03:29:04 -05:00
|
|
|
* @param {String} options.uri URI relative to the base uri to perform the GET request with.
|
|
|
|
* @param {String} options.type (optional) The type of data that you're expecting back from the server: json, xml, text. Default: json.
|
2013-10-29 07:19:27 -04:00
|
|
|
*/
|
2013-11-08 03:29:04 -05:00
|
|
|
RestDAO.prototype.get = function(options, callback) {
|
2014-06-06 12:36:23 -04:00
|
|
|
options.method = 'GET';
|
|
|
|
this._processRequest(options, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST (create) request
|
|
|
|
*/
|
|
|
|
RestDAO.prototype.post = function(item, uri, callback) {
|
|
|
|
this._processRequest({
|
|
|
|
method: 'POST',
|
|
|
|
payload: item,
|
|
|
|
uri: uri
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PUT (update) request
|
|
|
|
*/
|
|
|
|
RestDAO.prototype.put = function(item, uri, callback) {
|
|
|
|
this._processRequest({
|
|
|
|
method: 'PUT',
|
|
|
|
payload: item,
|
|
|
|
uri: uri
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE (remove) request
|
|
|
|
*/
|
|
|
|
RestDAO.prototype.remove = function(uri, callback) {
|
|
|
|
this._processRequest({
|
|
|
|
method: 'DELETE',
|
|
|
|
uri: uri
|
|
|
|
}, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
|
|
|
|
RestDAO.prototype._processRequest = function(options, callback) {
|
|
|
|
var xhr, format;
|
2013-11-08 03:29:04 -05:00
|
|
|
|
|
|
|
if (typeof options.uri === 'undefined') {
|
|
|
|
callback({
|
|
|
|
code: 400,
|
|
|
|
errMsg: 'Bad Request! URI is a mandatory parameter.'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.type = options.type || 'json';
|
|
|
|
|
|
|
|
if (options.type === 'json') {
|
2014-06-06 12:36:23 -04:00
|
|
|
format = 'application/json';
|
2013-11-08 03:29:04 -05:00
|
|
|
} else if (options.type === 'xml') {
|
2014-06-06 12:36:23 -04:00
|
|
|
format = 'application/xml';
|
2013-11-08 03:29:04 -05:00
|
|
|
} else if (options.type === 'text') {
|
2014-06-06 12:36:23 -04:00
|
|
|
format = 'text/plain';
|
2013-11-08 03:29:04 -05:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
code: 400,
|
|
|
|
errMsg: 'Bad Request! Unhandled data type.'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-28 14:14:32 -05:00
|
|
|
xhr = new XMLHttpRequest();
|
2014-06-06 12:36:23 -04:00
|
|
|
xhr.open(options.method, this._baseUri + options.uri);
|
|
|
|
xhr.setRequestHeader('Accept', format);
|
|
|
|
xhr.setRequestHeader('Content-Type', format);
|
2014-02-28 14:14:32 -05:00
|
|
|
|
|
|
|
xhr.onload = function() {
|
2014-06-06 12:36:23 -04:00
|
|
|
var res;
|
|
|
|
|
|
|
|
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) {
|
2014-02-28 14:14:32 -05:00
|
|
|
if (options.type === 'json') {
|
2014-06-06 12:36:23 -04:00
|
|
|
res = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText;
|
2014-02-28 14:14:32 -05:00
|
|
|
} else {
|
|
|
|
res = xhr.responseText;
|
|
|
|
}
|
|
|
|
|
2013-11-20 06:17:21 -05:00
|
|
|
callback(null, res, xhr.status);
|
2014-02-28 14:14:32 -05:00
|
|
|
return;
|
2013-10-29 07:19:27 -04:00
|
|
|
}
|
2014-02-28 14:14:32 -05:00
|
|
|
|
|
|
|
callback({
|
|
|
|
code: xhr.status,
|
|
|
|
errMsg: xhr.statusText
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-03-11 12:39:56 -04:00
|
|
|
xhr.onerror = function() {
|
2014-02-28 14:14:32 -05:00
|
|
|
callback({
|
2014-05-23 04:52:34 -04:00
|
|
|
code: 42,
|
2014-06-06 12:36:23 -04:00
|
|
|
errMsg: 'Error calling ' + options.method + ' on ' + options.uri
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-06 12:36:23 -04:00
|
|
|
xhr.send(options.payload ? JSON.stringify(options.payload) : undefined);
|
2013-10-29 07:19:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return RestDAO;
|
|
|
|
});
|