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-02-28 14:14:32 -05:00
|
|
|
var xhr, acceptHeader;
|
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') {
|
|
|
|
acceptHeader = 'application/json';
|
|
|
|
} else if (options.type === 'xml') {
|
|
|
|
acceptHeader = 'application/xml';
|
|
|
|
} else if (options.type === 'text') {
|
|
|
|
acceptHeader = 'text/plain';
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
code: 400,
|
|
|
|
errMsg: 'Bad Request! Unhandled data type.'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-28 14:14:32 -05:00
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', this._baseUri + options.uri);
|
|
|
|
xhr.setRequestHeader('Accept', acceptHeader);
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
var res;
|
|
|
|
if (options.type === 'json') {
|
|
|
|
res = JSON.parse(xhr.responseText);
|
|
|
|
} 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-03-11 12:39:56 -04:00
|
|
|
code: 404,
|
|
|
|
errMsg: 'Error calling GET on ' + options.uri
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
2013-10-29 07:19:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PUT (create/update) request
|
|
|
|
*/
|
|
|
|
RestDAO.prototype.put = function(item, uri, callback) {
|
2014-02-28 14:14:32 -05:00
|
|
|
var xhr;
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('PUT', this._baseUri + uri);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 201 || xhr.status === 304)) {
|
|
|
|
callback(null, xhr.responseText, xhr.status);
|
|
|
|
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-03-11 12:39:56 -04:00
|
|
|
errMsg: 'Error calling PUT on ' + uri
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify(item));
|
2013-10-29 07:19:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE (remove) request
|
|
|
|
*/
|
|
|
|
RestDAO.prototype.remove = function(uri, callback) {
|
2014-02-28 14:14:32 -05:00
|
|
|
var xhr;
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('DELETE', this._baseUri + uri);
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
callback(null, xhr.responseText, xhr.status);
|
|
|
|
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-03-11 12:39:56 -04:00
|
|
|
errMsg: 'Error calling DELETE on ' + uri
|
2014-02-28 14:14:32 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
2013-10-29 07:19:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return RestDAO;
|
|
|
|
});
|