Skip to content

Commit 5e2a13b

Browse files
committed
Adds Post model and basic gui
1 parent a26d5cf commit 5e2a13b

10 files changed

Lines changed: 226 additions & 0 deletions

File tree

client/app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<script src="scripts/controllers/users.js"></script>
6161
<script src="scripts/controllers/sandbox.js"></script>
6262
<script src="scripts/controllers/notes.js"></script>
63+
<script src="scripts/controllers/posts.js"></script>
6364
<!-- endbuild -->
6465
</body>
6566
</html>

client/app/scripts/controllers/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ angular.module('loopbackApp')
2525
$scope.menuoptions = [{
2626
name: 'Home',
2727
sref: 'app.home'
28+
} , {
29+
name: 'Posts',
30+
sref: 'app.posts.list'
2831
} , {
2932
name: 'Items',
3033
sref: 'app.items.list'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
});

client/app/views/posts/form.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h4>
2+
<a href="" ui-sref="^.list" class="btn btn-default"><i class="fa fa-arrow-left"></i></a>
3+
4+
<span ng-show="!post.id">Add a new post</span>
5+
<span ng-show="post.id">Edit post</span>
6+
7+
</h4>
8+
9+
10+
<div class="row">
11+
<div class="col-md-4">
12+
<div class="alert alert-danger" ng-if="addError">Could not add post!</div>
13+
<formly-form result="post" fields="formFields" options="formOptions" ng-submit="onSubmit()"></formly-form>
14+
</div>
15+
</div>

client/app/views/posts/list.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
<div class='list-group'>
4+
<div ng-repeat="item in posts" class="list-group-item">
5+
6+
<span class="pull-right btn-group" style="margin-top: 7px;">
7+
<a href="" class="btn btn-sm btn-default" ui-sref="^.edit({id: item.id})">
8+
<i class="fa fa-pencil"></i>
9+
</a>
10+
<a href="" class="btn btn-sm btn-default" ng-click="delete({id: item.id})">
11+
<i class="fa fa-trash-o"></i>
12+
</a>
13+
</span>
14+
15+
<h4 class="list-group-item-heading">
16+
<a href="" ui-sref="^.view({id: item.id})">{{item.title}}</a>
17+
</h4>
18+
<p class="list-group-item-text">{{item.body}}</p>
19+
20+
</div>
21+
22+
<div ng-show="!posts.length" class="list-group-item">
23+
<h4 class="list-group-item-heading">
24+
There are no posts
25+
</h4>
26+
<p class="list-group-item-text">Click <a href="" ui-sref="^.add">here</a> to add one!</p>
27+
</div>
28+
29+
</div>

client/app/views/posts/main.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
<h2 class="page-header">
3+
Posts
4+
<small>Manage your posts here!</small>
5+
<a ui-sref=".add" class="btn btn-default pull-right">Add</a>
6+
</h2>
7+
8+
<div ui-view></div>

client/app/views/posts/view.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<h4>
3+
<a href="" ui-sref="^.list" class="btn btn-default"><i class="fa fa-arrow-left"></i></a>
4+
{{post.title}}
5+
6+
<span class="pull-right btn-group" style="margin-top: 7px;">
7+
<a href="" class="btn btn-sm btn-default" ui-sref="^.edit({id: post.id})">
8+
<i class="fa fa-pencil"></i>
9+
</a>
10+
<a href="" class="btn btn-sm btn-default" ng-click="delete({id: post.id})">
11+
<i class="fa fa-trash-o"></i>
12+
</a>
13+
</span>
14+
15+
</h4>
16+
<hr>
17+
<pre>{{post | json}}</pre>

common/models/post.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "Post",
3+
"plural": "posts",
4+
"base": "PersistedModel",
5+
"properties": {
6+
"title": {
7+
"type": "string",
8+
"required": true
9+
},
10+
"content": {
11+
"type": "string"
12+
},
13+
"image": {
14+
"type": "string"
15+
},
16+
"created": {
17+
"type": "date"
18+
}
19+
},
20+
"validations": [],
21+
"relations": {},
22+
"acls": [],
23+
"methods": []
24+
}

server/model-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@
3232
"Note": {
3333
"dataSource": "db",
3434
"public": true
35+
},
36+
"Post": {
37+
"dataSource": "db",
38+
"public": true
3539
}
3640
}

test/spec/controllers/posts.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
describe('Controller: PostsCtrl', function () {
4+
5+
// load the controller's module
6+
beforeEach(module('loopbackApp'));
7+
8+
var PostsCtrl,
9+
scope;
10+
11+
// Initialize the controller and a mock scope
12+
beforeEach(inject(function ($controller, $rootScope) {
13+
scope = $rootScope.$new();
14+
PostsCtrl = $controller('PostsCtrl', {
15+
$scope: scope
16+
});
17+
}));
18+
19+
it('should attach a list of awesomeThings to the scope', function () {
20+
expect(scope.awesomeThings.length).toBe(3);
21+
});
22+
});

0 commit comments

Comments
 (0)