mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-01-17 21:02:22 +01:00
Extends the ArticlesService $resource object to include a custom method for creating, or updating, an Article instance. Related #1260
51 lines
1.2 KiB
JavaScript
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;
|
|
}
|
|
}
|
|
}
|
|
}());
|