refactor get/post into base method

This commit is contained in:
Mo Bitar
2017-02-26 10:31:20 -06:00
parent 1c51719bb4
commit fa02068784
4 changed files with 38 additions and 41 deletions

View File

@@ -1,5 +1,6 @@
angular.module('app.frontend')
.controller('HomeCtrl', function ($scope, $rootScope, $timeout, modelManager, syncManager, authManager) {
syncManager.loadLocalItems(function(items) {
$scope.$apply();

View File

@@ -30,6 +30,7 @@ class GlobalExtensionsMenu {
alert("Unable to register this extension. Make sure the link is valid and try again.");
}
} else {
console.log("Added extension");
$scope.newExtensionData.url = "";
$scope.showNewExtensionForm = false;
}

View File

@@ -92,7 +92,7 @@ class ExtensionManager {
*/
retrieveExtensionFromServer(url, callback) {
this.httpManager.getAbsolute(url, {}, function(response){
var ext = this.handleExtensionLoadExternalResponseItem(url, response.plain());
var ext = this.handleExtensionLoadExternalResponseItem(url, response);
if(callback) {
callback(ext);
}
@@ -318,7 +318,7 @@ class ExtensionManager {
this.httpManager.postAbsolute(action.url, params, function(response){
action.error = false;
if(callback) {
callback(response.plain());
callback(response);
}
}.bind(this), function(response){
action.error = true;

View File

@@ -1,7 +1,8 @@
class HttpManager {
setBaseUrl(baseUrl) {
this.baseUrl = baseUrl;
constructor($timeout) {
// calling callbacks in a $timeout allows angular UI to update
this.$timeout = $timeout;
}
setAuthHeadersForRequest(request) {
@@ -12,42 +13,14 @@ class HttpManager {
}
postAbsolute(url, params, onsuccess, onerror) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
console.log("on ready state", xmlhttp.readyState, "text:", xmlhttp.responseText);
var response = xmlhttp.responseText;
if(response) {
response = JSON.parse(response);
}
if(xmlhttp.status == 200){
console.log("onsuccess", response);
onsuccess(response);
} else {
console.error("onerror", response);
onerror(response)
}
}
}
xmlhttp.open("post", url, true);
this.setAuthHeadersForRequest(xmlhttp);
xmlhttp.setRequestHeader('Content-type', 'application/json');
xmlhttp.send(JSON.stringify(params));
this.httpRequest("post", url, params, onsuccess, onerror);
}
getAbsolute(url, params, onsuccess, onerror) {
this.httpRequest("get", url, params, onsuccess, onerror);
}
var formatParams = function(params) {
return "?" + Object
.keys(params)
.map(function(key){
return key+"="+params[key]
})
.join("&")
}
httpRequest(verb, url, params, onsuccess, onerror) {
var xmlhttp = new XMLHttpRequest();
@@ -60,18 +33,40 @@ class HttpManager {
if(xmlhttp.status == 200){
console.log("onsuccess", response);
onsuccess(response);
this.$timeout(function(){
onsuccess(response);
})
} else {
console.error("onerror", response);
onerror(response)
this.$timeout(function(){
onerror(response)
})
}
}
}.bind(this)
if(verb == "get") {
url = url + this.formatParams(params);
}
console.log("getting", url + formatParams(params));
xmlhttp.open("get", url + formatParams(params), true);
xmlhttp.open(verb, url, true);
this.setAuthHeadersForRequest(xmlhttp);
xmlhttp.setRequestHeader('Content-type', 'application/json');
xmlhttp.send();
if(verb == "post") {
xmlhttp.send(JSON.stringify(params));
} else {
xmlhttp.send();
}
}
formatParams(params) {
return "?" + Object
.keys(params)
.map(function(key){
return key+"="+params[key]
})
.join("&")
}
}