1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-07 23:17:25 +00:00

Moved api call to a service

This commit is contained in:
2016-03-02 13:34:41 +00:00
parent 459585fce2
commit 817e1fb0c1
6 changed files with 35 additions and 20 deletions

View File

@@ -1,17 +0,0 @@
app.controller("homeController", function($scope, $http) {
$scope.message = "Test message in scope.";
$http({
method: 'GET',
url: '/api/tournaments/'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
$scope.query = response.data;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.query = "Nothing there";
});
});

View File

@@ -0,0 +1,10 @@
app.controller("tournamentsController", function($scope, tournamentsService) {
$scope.message = "Test message in scope.";
tournamentsService.getAllTournaments().then(function(data) {
//this will execute when the
//AJAX call completes.
console.log(data);
$scope.allTournaments = data;
});
});

View File

@@ -0,0 +1,17 @@
app.factory('tournamentsService', function($http){
return {
getAllTournaments: function() {
//since $http.get returns a promise,
//and promise.then() also returns a promise
//that resolves to whatever value is returned in it's
//callback argument, we can return that.
return $http.get('/api/tournaments/').then(function(result) {
return result.data;
});
}
};
});