Skip to content

Commit cc3808c

Browse files
committed
Start implementing resolves in router using Service
1 parent deb81f8 commit cc3808c

4 files changed

Lines changed: 49 additions & 51 deletions

File tree

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
'use strict';
2-
angular.module('com.module.posts')
3-
.config(function($stateProvider) {
4-
$stateProvider.state('app.posts', {
5-
abstract: true,
6-
url: '/posts',
7-
templateUrl: 'modules/posts/views/main.html'
8-
})
9-
.state('app.posts.list', {
10-
url: '',
11-
templateUrl: 'modules/posts/views/list.html',
12-
controller: 'PostsCtrl'
13-
})
14-
.state('app.posts.add', {
15-
url: '/add',
16-
templateUrl: 'modules/posts/views/form.html',
17-
controller: 'PostsCtrl'
18-
})
19-
.state('app.posts.edit', {
20-
url: '/:id/edit',
21-
templateUrl: 'modules/posts/views/form.html',
22-
controller: 'PostsCtrl'
23-
})
24-
.state('app.posts.view', {
25-
url: '/:id',
26-
templateUrl: 'modules/posts/views/view.html',
27-
controller: 'PostsCtrl'
28-
});
2+
3+
var app = angular.module('com.module.posts');
4+
5+
app.config(function ($stateProvider) {
6+
$stateProvider.state('app.posts', {
7+
abstract: true,
8+
url: '/posts',
9+
templateUrl: 'modules/posts/views/main.html'
10+
}).state('app.posts.list', {
11+
url: '',
12+
templateUrl: 'modules/posts/views/list.html',
13+
controller: 'PostsCtrl',
14+
controllerAs: 'ctrl',
15+
resolve: {
16+
posts: ['PostsService', function (PostsService) {
17+
return PostsService.getPosts();
18+
}]
19+
}
20+
}).state('app.posts.add', {
21+
url: '/add',
22+
templateUrl: 'modules/posts/views/form.html',
23+
controller: 'PostsCtrl'
24+
}).state('app.posts.edit', {
25+
url: '/:id/edit',
26+
templateUrl: 'modules/posts/views/form.html',
27+
controller: 'PostsCtrl'
28+
}).state('app.posts.view', {
29+
url: '/:id',
30+
templateUrl: 'modules/posts/views/view.html',
31+
controller: 'PostsCtrl'
2932
});
33+
});

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
angular.module('com.module.posts')
3-
.controller('PostsCtrl', function ($scope, $state, $stateParams, CoreService, gettextCatalog, Post) {
3+
.controller('PostsCtrl', function ($scope, $state, $stateParams, CoreService, gettextCatalog, PostsService, Post) {
44

55
var postId = $stateParams.id;
66

@@ -15,17 +15,10 @@ angular.module('com.module.posts')
1515
$scope.post = {};
1616
}
1717

18-
function loadItems() {
19-
$scope.posts = Post.find();
20-
}
21-
22-
loadItems();
23-
2418
$scope.delete = function (id) {
2519
CoreService.confirm(gettextCatalog.getString('Are you sure?'), gettextCatalog.getString('Deleting this cannot be undone'), function () {
2620
Post.deleteById(id, function () {
2721
CoreService.toastSuccess(gettextCatalog.getString('Post deleted'), gettextCatalog.getString('Your post is deleted!'));
28-
loadItems();
2922
$state.go('app.posts.list');
3023
}, function (err) {
3124
CoreService.toastError(gettextCatalog.getString('Error deleting post'), gettextCatalog.getString('Your post is not deleted: ') + err);
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
'use strict';
2-
angular.module('com.module.posts')
3-
.service('PostsService', ['$http', 'Post', function ($http, Post) {
4-
this.savePost = function (goat) {
5-
return $http.post('/goats', goat);
6-
};
2+
var app = angular.module('com.module.posts');
73

8-
this.getPosts = function () {
9-
return Post.find();
10-
};
4+
app.service('PostsService', ['$http', 'Post', function ($http, Post) {
5+
this.savePost = function (goat) {
6+
return $http.post('/goats', goat);
7+
};
118

12-
this.getPost = function (postId) {
13-
return Post.findById({
14-
id: postId
15-
});
16-
};
17-
}]);
9+
this.getPosts = function () {
10+
return Post.find().$promise;
11+
};
12+
13+
this.getPost = function (postId) {
14+
return Post.findById({
15+
id: postId
16+
});
17+
};
18+
}]);

client/app/modules/posts/views/list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
<div class='list-group'>
4-
<div ng-repeat="item in posts" class="list-group-item">
4+
<div ng-repeat="item in ctrl.posts" class="list-group-item">
55

66
<span class="pull-right btn-group" style="margin-top: 7px;">
77
<a href="" class="btn btn-sm btn-default" ui-sref="^.edit({id: item.id})">
@@ -19,7 +19,7 @@ <h4 class="list-group-item-heading">
1919

2020
</div>
2121

22-
<div ng-show="!posts.length" class="list-group-item">
22+
<div ng-show="!ctrl.posts.length" class="list-group-item">
2323
<h4 class="list-group-item-heading" translate>
2424
There are no posts
2525
</h4>

0 commit comments

Comments
 (0)