Last active
April 25, 2017 09:02
-
-
Save oliverm2112/5414702 to your computer and use it in GitHub Desktop.
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
app.factory('httpService', ['$http', '$rootScope', function ($http, $rootScope) { | |
var service = {}, | |
name; | |
const GATEWAY = 'http://localhost:3000/'; | |
const CFGATEWAY = 'http://localhost:8501/angular/gateway.cfm'; | |
$rootScope.$on('post.test', function (event, data) { | |
name = event.name; | |
service.post(data); | |
}); | |
$rootScope.$on('ping', function (event, data) { | |
name = event.name; | |
service.head(data.url); | |
}); | |
$rootScope.$on('crew.get', function (event, data) { | |
name = event.name; | |
console.log('on ' + name, 'event:', event, 'data:', data); | |
service.get(data.method, data.params, data.cache); | |
}); | |
service.post = function (data) { | |
$http({ | |
method : 'POST', | |
url : CFGATEWAY, | |
data : data | |
}) | |
.success(function (data, status, headers, config) { | |
console.log('Post OK!!!', data); | |
$rootScope.$broadcast(name + '.success', data); | |
}) | |
.error(function (data, status, headers, config) { | |
console.log('POST error', data); | |
$rootScope.$broadcast(name + '.error', 'data:', data, 'status:', status, 'headers:', headers); | |
}); | |
}; | |
service.get = function (method, params, cache) { | |
$http({ | |
method : 'GET', | |
url : GATEWAY + method, | |
params : {param1 : 'hello'}, | |
cache : cache ? true : false | |
} | |
) | |
.success(function (data, status, headers, config) { | |
$rootScope.$broadcast(name + '.success', data); | |
}) | |
.error(function (data, status, headers, config) { | |
console.log('GET error', data); | |
$rootScope.$broadcast(name + '.error', data); | |
}); | |
}; | |
service.head = function (url) { | |
$http({ | |
method : 'HEAD', | |
url : url, | |
params : {param1 : 'hello'}, | |
cache : false | |
} | |
) | |
.success(function (data, status, headers, config) { | |
$rootScope.$broadcast(name + '.success', data); | |
}) | |
.error(function (data, status, headers, config) { | |
console.log('HEAD error'); | |
$rootScope.$broadcast(name + '.error', data); | |
} | |
); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, It let me work.