|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc function |
| 5 | + * @name loopbackApp.controller:PostsCtrl |
| 6 | + * @description |
| 7 | + * # PostsCtrl |
| 8 | + * Controller of the loopbackApp |
| 9 | + */ |
| 10 | +angular.module('loopbackApp') |
| 11 | + |
| 12 | + .config(function($stateProvider) { |
| 13 | + $stateProvider.state('app.posts', { |
| 14 | + abstract: true, |
| 15 | + url: '/posts', |
| 16 | + templateUrl: 'views/posts/main.html', |
| 17 | + controller: 'PostsCtrl' |
| 18 | + }) |
| 19 | + .state('app.posts.list', { |
| 20 | + url: '', |
| 21 | + templateUrl: 'views/posts/list.html', |
| 22 | + controller: 'PostsCtrl' |
| 23 | + }) |
| 24 | + .state('app.posts.add', { |
| 25 | + url: '/add', |
| 26 | + templateUrl: 'views/posts/form.html', |
| 27 | + controller: 'PostsCtrl' |
| 28 | + }) |
| 29 | + .state('app.posts.edit', { |
| 30 | + url: '/:id/edit', |
| 31 | + templateUrl: 'views/posts/form.html', |
| 32 | + controller: 'PostsCtrl' |
| 33 | + }) |
| 34 | + .state('app.posts.view', { |
| 35 | + url: '/:id', |
| 36 | + templateUrl: 'views/posts/view.html', |
| 37 | + controller: 'PostsCtrl' |
| 38 | + }); |
| 39 | + }) |
| 40 | + |
| 41 | + .controller('PostsCtrl', function($scope, $state, $stateParams, toasty, Post) { |
| 42 | + |
| 43 | + var postId = $stateParams.id; |
| 44 | + |
| 45 | + if (postId) { |
| 46 | + $scope.post = Post.findById({ |
| 47 | + id: postId |
| 48 | + }, function() {}, function(err) { |
| 49 | + console.log(err); |
| 50 | + }); |
| 51 | + } else { |
| 52 | + $scope.post = {}; |
| 53 | + } |
| 54 | + |
| 55 | + function loadItems() { |
| 56 | + $scope.posts = Post.find(); |
| 57 | + } |
| 58 | + |
| 59 | + loadItems(); |
| 60 | + |
| 61 | + $scope.delete = function(id) { |
| 62 | + // if (confirm('Are you sure?') === false) { |
| 63 | + // return false; |
| 64 | + // } |
| 65 | + Post.deleteById(id, function() { |
| 66 | + toasty.pop.success({title: 'Post deleted', msg: 'Your post is deleted!', sound: false}); |
| 67 | + loadItems(); |
| 68 | + $state.go('app.posts.list'); |
| 69 | + console.log(); |
| 70 | + }, function(err) { |
| 71 | + toasty.pop.error({title: 'Error deleting post', msg: 'Your post is not deleted: ' + err, sound: false}); |
| 72 | + }); |
| 73 | + |
| 74 | + }; |
| 75 | + |
| 76 | + $scope.formFields = [{ |
| 77 | + key: 'title', |
| 78 | + type: 'text', |
| 79 | + label: 'Title', |
| 80 | + required: true |
| 81 | + }, { |
| 82 | + key: 'body', |
| 83 | + type: 'text', |
| 84 | + label: 'Body', |
| 85 | + required: true |
| 86 | + }]; |
| 87 | + |
| 88 | + $scope.formOptions = { |
| 89 | + uniqueFormId: true, |
| 90 | + hideSubmit: false, |
| 91 | + submitCopy: 'Save' |
| 92 | + }; |
| 93 | + |
| 94 | + $scope.onSubmit = function() { |
| 95 | + Post.upsert($scope.post, function() { |
| 96 | + toasty.pop.success({title: 'Post saved', msg: 'Your post is safe with us!', sound: false}); |
| 97 | + $state.go('^.list'); |
| 98 | + }, function(err) { |
| 99 | + console.log(err); |
| 100 | + }); |
| 101 | + }; |
| 102 | + |
| 103 | + }); |
0 commit comments