mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-10 23:53:06 +00:00
Moved api call to a service
This commit is contained in:
@@ -12,4 +12,9 @@ class ApiController < ApplicationController
|
|||||||
def tournament
|
def tournament
|
||||||
@tournament = Tournament.where(:id => params[:tournament]).includes(:schools,:weights,:mats,:matches,:user,:wrestlers).first
|
@tournament = Tournament.where(:id => params[:tournament]).includes(:schools,:weights,:mats,:matches,:user,:wrestlers).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def newTournament
|
||||||
|
@tournament = Tournament.new(JSON.parse(params[:tournament]))
|
||||||
|
@tournament.save
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ Wrestling::Application.routes.draw do
|
|||||||
get "/api/tournaments" => "api#tournaments"
|
get "/api/tournaments" => "api#tournaments"
|
||||||
get "/api/tournaments/:tournament" => "api#tournament"
|
get "/api/tournaments/:tournament" => "api#tournament"
|
||||||
get "/api/index" => "api#index"
|
get "/api/index" => "api#index"
|
||||||
|
post "/api/tournaments/new" => "newTournament"
|
||||||
|
|
||||||
# Example of regular route:
|
# Example of regular route:
|
||||||
# get 'products/:id' => 'catalog#view'
|
# get 'products/:id' => 'catalog#view'
|
||||||
|
|||||||
@@ -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";
|
|
||||||
});
|
|
||||||
});
|
|
||||||
10
frontend/app/js/tournaments-controller.js
Normal file
10
frontend/app/js/tournaments-controller.js
Normal 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;
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
17
frontend/app/js/tournaments-service.js
Normal file
17
frontend/app/js/tournaments-service.js
Normal 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
<!--leftsidebar-->
|
<!--leftsidebar-->
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-8" style="padding-left: 2%;">
|
<div class="col-md-8" style="padding-left: 2%;">
|
||||||
<div class="container" ui-view="main" ng-controller="homeController">
|
<div class="container" ui-view="main" ng-controller="tournamentsController">
|
||||||
<h2>All Tournaments</h2>
|
<h2>All Tournaments</h2>
|
||||||
<br>
|
<br>
|
||||||
<table class="table">
|
<table class="table">
|
||||||
@@ -49,7 +49,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr ng-repeat="tournament in query">
|
<tr ng-repeat="tournament in allTournaments">
|
||||||
<td>{{ tournament.name }}</td>
|
<td>{{ tournament.name }}</td>
|
||||||
<td>{{ tournament.address }}</td>
|
<td>{{ tournament.address }}</td>
|
||||||
<td>{{ tournament.director }}</td>
|
<td>{{ tournament.director }}</td>
|
||||||
@@ -65,7 +65,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
<!-- Latest compiled and minified JavaScript -->
|
<!-- Latest compiled and minified JavaScript -->
|
||||||
|
|||||||
Reference in New Issue
Block a user