Skip to content

Commit a11c9b6

Browse files
committed
Add more logic to service and routes
1 parent fe1bd0f commit a11c9b6

3 files changed

Lines changed: 48 additions & 18 deletions

File tree

client/app/modules/posts/config/posts.routes.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ app.config(function ($stateProvider) {
77
abstract: true,
88
url: '/posts',
99
templateUrl: 'modules/posts/views/main.html',
10-
controller: 'PostsCtrl'
10+
controller: 'PostsCtrl',
11+
controllerAs: 'ctrl'
1112
}).state('app.posts.list', {
1213
url: '',
1314
templateUrl: 'modules/posts/views/list.html',
@@ -30,6 +31,13 @@ app.config(function ($stateProvider) {
3031
}).state('app.posts.view', {
3132
url: '/:id',
3233
templateUrl: 'modules/posts/views/view.html',
33-
controller: 'PostsCtrl'
34+
resolve: {
35+
post: ['$stateParams', 'PostsService', function ($stateParams, PostsService) {
36+
return PostsService.getPost($stateParams.id);
37+
}]
38+
},
39+
controller: function ($scope, post) {
40+
$scope.post = post;
41+
}
3442
});
3543
});

client/app/modules/posts/controllers/posts.ctrl.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
'use strict';
22
angular.module('com.module.posts')
3-
.controller('PostsCtrl', function ($scope, $state, $stateParams, CoreService, gettextCatalog, PostsService, Post) {
3+
.controller('PostsCtrl', function ($scope, $state, $stateParams, CoreService, FormHelper, gettextCatalog, Post, PostsService) {
4+
5+
$scope.delete = function (id) {
6+
PostsService.deletePost(id, function () {
7+
$state.reload();
8+
});
9+
};
10+
11+
this.formHelper = new FormHelper(Post);
12+
$scope.cancel = function () {
13+
console.log('Cancel');
14+
console.log(this.formHelper);
15+
//this.formHelper.cancel('app.posts.list');
16+
};
417

518
var postId = $stateParams.id;
619

@@ -15,19 +28,6 @@ angular.module('com.module.posts')
1528
$scope.post = {};
1629
}
1730

18-
$scope.delete = function (id) {
19-
CoreService.confirm(gettextCatalog.getString('Are you sure?'), gettextCatalog.getString('Deleting this cannot be undone'), function () {
20-
Post.deleteById(id, function () {
21-
CoreService.toastSuccess(gettextCatalog.getString('Post deleted'), gettextCatalog.getString('Your post is deleted!'));
22-
$state.reload();
23-
}, function (err) {
24-
CoreService.toastError(gettextCatalog.getString('Error deleting post'), gettextCatalog.getString('Your post is not deleted: ') + err);
25-
});
26-
}, function () {
27-
return false;
28-
});
29-
};
30-
3131
$scope.formFields = [
3232
{
3333
key: 'title',
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
'use strict';
22
var app = angular.module('com.module.posts');
33

4-
app.service('PostsService', ['Post', function (Post) {
4+
app.service('PostsService', ['CoreService', 'gettextCatalog', 'Post', function (CoreService, gettextCatalog, Post) {
55

66
this.getPosts = function () {
7-
return Post.find().$promise;
7+
return Post.find({
8+
filter: {
9+
order: 'created DESC'
10+
}
11+
}).$promise;
812
};
913

14+
this.getPost = function (id) {
15+
return Post.findById({id: id}).$promise;
16+
};
17+
18+
this.deletePost = function (id, cb) {
19+
CoreService.confirm(gettextCatalog.getString('Are you sure?'), gettextCatalog.getString('Deleting this cannot be undone'), function () {
20+
Post.deleteById(id, function () {
21+
CoreService.toastSuccess(gettextCatalog.getString('Item deleted'), gettextCatalog.getString('Your item has been deleted!'));
22+
cb();
23+
}, function (err) {
24+
CoreService.toastError(gettextCatalog.getString('Oops'), gettextCatalog.getString('Error deleting item: ') + err);
25+
cb();
26+
});
27+
}, function () {
28+
return false;
29+
});
30+
}
31+
1032
}]);

0 commit comments

Comments
 (0)