Skip to content

Instantly share code, notes, and snippets.

@spsaucier

spsaucier/app.js Secret

Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spsaucier/5029e9043c1bd0517866 to your computer and use it in GitHub Desktop.
Save spsaucier/5029e9043c1bd0517866 to your computer and use it in GitHub Desktop.
FFA_SW 4
.controller('FilmsCtrl', ["$scope", "$state", "$http", function($scope, $state, $http){
// Grab URL parameters - this is unique to FFA, not standard for Angular - ensure $state is included in your dependencies list in the controller definition above
$scope.id = ($state.params.id || '');
$scope.page = ($state.params.p || 1);
// If we're on the first page or page is set to default
if ($scope.page == 1) {
if ($scope.id != '') {
// We've got a URL parameter, so let's get the single entity's data from our data source
$http.get("http://swapi.co/api/"+'films'+"/"+$scope.id, { cache: true })
.success(function(data) {
// If the request succeeds, assign our data to the 'film' variable, passed to our page through $scope
$scope['film'] = data;
})
} else {
// There is no ID, so we'll show a list of all films.
// We're on page 1, so the next page is 2.
$http.get("http://swapi.co/api/"+'films'+"/", { cache: true }).success(function(data) {
$scope['films'] = data;
if (data['next']) $scope.nextPage = 2;
});
}
} else {
// Once again, there is no ID, so we'll show a list of all films.
// If there's a next page, let's add it. Otherwise just add the previous page button.
$http.get("http://swapi.co/api/"+'films'+"/?page="+$scope.page, { cache: true }).success(function(data) {
$scope['films'] = data;
if (data['next']) $scope.nextPage = 1*$scope.page + 1;
});
$scope.prevPage = 1*$scope.page - 1;
}
return $scope;
}]) // Ensure you don't end in a semicolon, as there are more actions that follow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment