-
-
Save bbraithwaite/6463a379b11d263874f9 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
'use strict'; | |
// Categories controller | |
angular.module('categories').controller('CategoriesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Categories', | |
function($scope, $stateParams, $location, Authentication, Categories) { | |
$scope.authentication = Authentication; | |
// Create new Category | |
$scope.create = function() { | |
// Create new Category object | |
var category = new Categories ({ | |
name: this.name, | |
description: this.description | |
}); | |
// Redirect after save | |
category.$save(function(response) { | |
$location.path('categories/' + response._id); | |
// Clear form fields | |
$scope.name = ''; | |
}, function(errorResponse) { | |
$scope.error = errorResponse.data.message; | |
}); | |
}; | |
// Remove existing Category | |
$scope.remove = function(category) { | |
if ( category ) { | |
category.$remove(); | |
for (var i in $scope.categories) { | |
if ($scope.categories [i] === category) { | |
$scope.categories.splice(i, 1); | |
} | |
} | |
} else { | |
$scope.category.$remove(function() { | |
$location.path('categories'); | |
}); | |
} | |
}; | |
// Update existing Category | |
$scope.update = function() { | |
var category = $scope.category; | |
category.$update(function() { | |
$location.path('categories/' + category._id); | |
}, function(errorResponse) { | |
$scope.error = errorResponse.data.message; | |
}); | |
}; | |
// Find a list of Categories | |
$scope.find = function() { | |
$scope.categories = Categories.query(); | |
}; | |
// Find existing Category | |
$scope.findOne = function() { | |
$scope.category = Categories.get({ | |
categoryId: $stateParams.categoryId | |
}); | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment