feat(articles): Modify articles module to implement style guidelines.

Update the articles module to implement the style guidelines.

Much of this work is from @trainerbill

Closes #874
Closes #339
This commit is contained in:
Ryan Hutchison
2016-01-01 14:58:42 -05:00
parent 169d4cd3e2
commit b3ad56efa3
14 changed files with 511 additions and 260 deletions

View File

@@ -1,84 +1,52 @@
'use strict';
(function () {
'use strict';
// Articles controller
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
function ($scope, $stateParams, $location, Authentication, Articles) {
$scope.authentication = Authentication;
angular
.module('articles')
.controller('ArticlesController', ArticlesController);
// Create new Article
$scope.create = function (isValid) {
$scope.error = null;
ArticlesController.$inject = ['$scope', '$state', 'articleResolve', 'Authentication'];
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'articleForm');
function ArticlesController($scope, $state, article, Authentication) {
var vm = this;
return false;
}
// Create new Article object
var article = new Articles({
title: this.title,
content: this.content
});
// Redirect after save
article.$save(function (response) {
$location.path('articles/' + response._id);
// Clear form fields
$scope.title = '';
$scope.content = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
vm.article = article;
vm.authentication = Authentication;
vm.error = null;
vm.form = {};
vm.remove = remove;
vm.save = save;
// Remove existing Article
$scope.remove = function (article) {
if (article) {
article.$remove();
for (var i in $scope.articles) {
if ($scope.articles[i] === article) {
$scope.articles.splice(i, 1);
}
}
} else {
$scope.article.$remove(function () {
$location.path('articles');
});
function remove() {
if (confirm('Are you sure you want to delete?')) {
vm.article.$remove($state.go('articles.list'));
}
};
// Update existing Article
$scope.update = function (isValid) {
$scope.error = null;
}
// Save Article
function save(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'articleForm');
$scope.$broadcast('show-errors-check-validity', 'vm.form.articleForm');
return false;
}
var article = $scope.article;
// TODO: move create/update logic to service
if (vm.article._id) {
vm.article.$update(successCallback, errorCallback);
} else {
vm.article.$save(successCallback, errorCallback);
}
article.$update(function () {
$location.path('articles/' + article._id);
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
function successCallback(res) {
$state.go('articles.view', {
articleId: res._id
});
}
// Find a list of Articles
$scope.find = function () {
$scope.articles = Articles.query();
};
// Find existing Article
$scope.findOne = function () {
$scope.article = Articles.get({
articleId: $stateParams.articleId
});
};
function errorCallback(res) {
vm.error = res.data.message;
}
}
}
]);
})();