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