refactor get/post into base method
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
angular.module('app.frontend')
|
angular.module('app.frontend')
|
||||||
.controller('HomeCtrl', function ($scope, $rootScope, $timeout, modelManager, syncManager, authManager) {
|
.controller('HomeCtrl', function ($scope, $rootScope, $timeout, modelManager, syncManager, authManager) {
|
||||||
|
|
||||||
syncManager.loadLocalItems(function(items) {
|
syncManager.loadLocalItems(function(items) {
|
||||||
$scope.$apply();
|
$scope.$apply();
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class GlobalExtensionsMenu {
|
|||||||
alert("Unable to register this extension. Make sure the link is valid and try again.");
|
alert("Unable to register this extension. Make sure the link is valid and try again.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
console.log("Added extension");
|
||||||
$scope.newExtensionData.url = "";
|
$scope.newExtensionData.url = "";
|
||||||
$scope.showNewExtensionForm = false;
|
$scope.showNewExtensionForm = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ class ExtensionManager {
|
|||||||
*/
|
*/
|
||||||
retrieveExtensionFromServer(url, callback) {
|
retrieveExtensionFromServer(url, callback) {
|
||||||
this.httpManager.getAbsolute(url, {}, function(response){
|
this.httpManager.getAbsolute(url, {}, function(response){
|
||||||
var ext = this.handleExtensionLoadExternalResponseItem(url, response.plain());
|
var ext = this.handleExtensionLoadExternalResponseItem(url, response);
|
||||||
if(callback) {
|
if(callback) {
|
||||||
callback(ext);
|
callback(ext);
|
||||||
}
|
}
|
||||||
@@ -318,7 +318,7 @@ class ExtensionManager {
|
|||||||
this.httpManager.postAbsolute(action.url, params, function(response){
|
this.httpManager.postAbsolute(action.url, params, function(response){
|
||||||
action.error = false;
|
action.error = false;
|
||||||
if(callback) {
|
if(callback) {
|
||||||
callback(response.plain());
|
callback(response);
|
||||||
}
|
}
|
||||||
}.bind(this), function(response){
|
}.bind(this), function(response){
|
||||||
action.error = true;
|
action.error = true;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
class HttpManager {
|
class HttpManager {
|
||||||
|
|
||||||
setBaseUrl(baseUrl) {
|
constructor($timeout) {
|
||||||
this.baseUrl = baseUrl;
|
// calling callbacks in a $timeout allows angular UI to update
|
||||||
|
this.$timeout = $timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
setAuthHeadersForRequest(request) {
|
setAuthHeadersForRequest(request) {
|
||||||
@@ -12,42 +13,14 @@ class HttpManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
postAbsolute(url, params, onsuccess, onerror) {
|
postAbsolute(url, params, onsuccess, onerror) {
|
||||||
var xmlhttp = new XMLHttpRequest();
|
this.httpRequest("post", url, params, onsuccess, onerror);
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getAbsolute(url, params, onsuccess, onerror) {
|
getAbsolute(url, params, onsuccess, onerror) {
|
||||||
|
this.httpRequest("get", url, params, onsuccess, onerror);
|
||||||
|
}
|
||||||
|
|
||||||
var formatParams = function(params) {
|
httpRequest(verb, url, params, onsuccess, onerror) {
|
||||||
return "?" + Object
|
|
||||||
.keys(params)
|
|
||||||
.map(function(key){
|
|
||||||
return key+"="+params[key]
|
|
||||||
})
|
|
||||||
.join("&")
|
|
||||||
}
|
|
||||||
|
|
||||||
var xmlhttp = new XMLHttpRequest();
|
var xmlhttp = new XMLHttpRequest();
|
||||||
|
|
||||||
@@ -60,18 +33,40 @@ class HttpManager {
|
|||||||
|
|
||||||
if(xmlhttp.status == 200){
|
if(xmlhttp.status == 200){
|
||||||
console.log("onsuccess", response);
|
console.log("onsuccess", response);
|
||||||
onsuccess(response);
|
this.$timeout(function(){
|
||||||
|
onsuccess(response);
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
console.error("onerror", response);
|
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);
|
this.setAuthHeadersForRequest(xmlhttp);
|
||||||
xmlhttp.setRequestHeader('Content-type', 'application/json');
|
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("&")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user