Files
meanTorrent/modules/articles/client/controllers/articles.client.controller.js
Michael Leanos a7370a7808 feat(articles): ArticlesService extended $resource (#1266)
Extends the ArticlesService $resource object to include a custom method
for creating, or updating, an Article instance.

Related #1260
2016-07-10 17:55:29 -07:00

51 lines
1.2 KiB
JavaScript

(function () {
'use strict';
angular
.module('articles')
.controller('ArticlesController', ArticlesController);
ArticlesController.$inject = ['$scope', '$state', 'articleResolve', '$window', 'Authentication'];
function ArticlesController($scope, $state, article, $window, Authentication) {
var vm = this;
vm.article = article;
vm.authentication = Authentication;
vm.error = null;
vm.form = {};
vm.remove = remove;
vm.save = save;
// Remove existing Article
function remove() {
if ($window.confirm('Are you sure you want to delete?')) {
vm.article.$remove($state.go('articles.list'));
}
}
// Save Article
function save(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.form.articleForm');
return false;
}
// Create a new article, or update the current instance
vm.article.createOrUpdate()
.then(successCallback)
.catch(errorCallback);
function successCallback(res) {
$state.go('articles.view', {
articleId: res._id
});
}
function errorCallback(res) {
vm.error = res.data.message;
}
}
}
}());