Skip to content

Commit c7f8406

Browse files
committed
Refactore Notes to use a service
1 parent 1869a9d commit c7f8406

4 files changed

Lines changed: 121 additions & 100 deletions

File tree

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
2-
angular.module('com.module.notes')
3-
.run(function ($rootScope, Note, gettextCatalog) {
4-
$rootScope.addMenu('Notes', 'app.notes.list', 'fa-file-o');
2+
var app = angular.module('com.module.notes');
53

6-
Note.find(function (data) {
7-
$rootScope.addDashboardBox(gettextCatalog.getString('Notes'), 'bg-green', 'ion-calendar', data.length, 'app.notes.list');
8-
});
4+
app.run(function ($rootScope, Note, gettextCatalog) {
5+
$rootScope.addMenu('Notes', 'app.notes.list', 'fa-file-o');
96

7+
Note.find(function (data) {
8+
$rootScope.addDashboardBox(gettextCatalog.getString('Notes'), 'bg-green', 'ion-clipboard', data.length, 'app.notes.list');
109
});
10+
11+
});
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
'use strict';
2-
angular.module ('com.module.notes')
3-
.config (function ($stateProvider) {
4-
$stateProvider.state ('app.notes', {
5-
abstract: true,
6-
url: '/notes',
7-
templateUrl: 'modules/notes/views/main.html',
8-
controller: 'NotesCtrl'
9-
})
10-
.state ('app.notes.list', {
11-
url: '',
12-
templateUrl: 'modules/notes/views/list.html',
13-
controller: 'NotesCtrl'
14-
})
15-
.state ('app.notes.add', {
16-
url: '/add',
17-
templateUrl: 'modules/notes/views/form.html',
18-
controller: 'NotesCtrl'
19-
})
20-
.state ('app.notes.edit', {
21-
url: '/:id/edit',
22-
templateUrl: 'modules/notes/views/form.html',
23-
controller: 'NotesCtrl'
24-
})
25-
.state ('app.notes.view', {
26-
url: '/:id',
27-
templateUrl: 'modules/notes/views/view.html',
28-
controller: 'NotesCtrl'
29-
});
2+
var app = angular.module('com.module.notes');
3+
4+
app.config(function ($stateProvider) {
5+
$stateProvider.state('app.notes', {
6+
abstract: true,
7+
url: '/notes',
8+
templateUrl: 'modules/notes/views/main.html',
9+
controller: 'NotesCtrl'
10+
}
11+
).state('app.notes.list', {
12+
url: '',
13+
templateUrl: 'modules/notes/views/list.html',
14+
controller: 'NotesCtrl'
15+
}
16+
).state('app.notes.add', {
17+
url: '/add',
18+
templateUrl: 'modules/notes/views/form.html',
19+
controller: 'NotesCtrl'
20+
}
21+
).state('app.notes.edit', {
22+
url: '/:id/edit',
23+
templateUrl: 'modules/notes/views/form.html',
24+
controller: 'NotesCtrl'
25+
}
26+
).state('app.notes.view', {
27+
url: '/:id',
28+
templateUrl: 'modules/notes/views/view.html',
29+
controller: 'NotesCtrl'
30+
}
31+
);
3032
});
Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,48 @@
11
'use strict';
2-
angular.module('com.module.notes')
3-
.controller('NotesCtrl', function ($scope, $state, $stateParams, CoreService, Note) {
4-
5-
var noteId = $stateParams.id;
6-
7-
if (noteId) {
8-
$scope.note = Note.findById({
9-
id: noteId
10-
}, function () {
11-
}, function (err) {
12-
console.log(err);
13-
});
14-
} else {
15-
$scope.note = {};
2+
var app = angular.module('com.module.notes');
3+
4+
app.controller('NotesCtrl', function ($scope, $state, $stateParams, NotesService) {
5+
6+
$scope.formFields = [
7+
{
8+
key: 'title',
9+
type: 'text',
10+
label: 'Title',
11+
required: true
12+
},
13+
{
14+
key: 'body',
15+
type: 'textarea',
16+
label: 'Body',
17+
required: true
1618
}
17-
18-
function loadItems() {
19-
$scope.notes = Note.find();
20-
}
21-
22-
loadItems();
23-
24-
$scope.delete = function (id) {
25-
CoreService.confirm('Are you sure?', 'Deleting this cannot be undone', function () {
26-
Note.deleteById(id, function () {
27-
CoreService.toastSuccess('Note deleted', 'Your note is deleted!');
28-
loadItems();
29-
$state.go('app.notes.list');
30-
}, function (err) {
31-
CoreService.toastError('Error deleting note', 'Your note is not deleted! ' + err);
32-
});
33-
}, function () {
34-
return false;
35-
});
36-
};
37-
38-
$scope.formFields = [
39-
{
40-
key: 'title',
41-
type: 'text',
42-
label: 'Title',
43-
required: true
44-
},
45-
{
46-
key: 'body',
47-
type: 'textarea',
48-
label: 'Body',
49-
required: true
50-
}
51-
];
52-
53-
$scope.formOptions = {
54-
uniqueFormId: true,
55-
hideSubmit: false,
56-
submitCopy: 'Save'
57-
};
58-
59-
$scope.onSubmit = function () {
60-
Note.upsert($scope.note, function () {
61-
CoreService.toastSuccess('Note saved', 'Your note is safe with us!');
62-
$state.go('^.list');
63-
}, function (err) {
64-
console.log(err);
65-
});
66-
};
67-
68-
});
19+
];
20+
21+
$scope.formOptions = {
22+
uniqueFormId: true,
23+
hideSubmit: false,
24+
submitCopy: 'Save'
25+
};
26+
27+
$scope.delete = function (id) {
28+
NotesService.deleteNote(id, function () {
29+
$scope.notes = NotesService.getNotes();
30+
});
31+
};
32+
33+
$scope.onSubmit = function () {
34+
NotesService.upsertNote($scope.note, function () {
35+
$scope.notes = NotesService.getNotes();
36+
$state.go('^.list');
37+
});
38+
};
39+
40+
$scope.notes = NotesService.getNotes();
41+
42+
if ($stateParams.id) {
43+
$scope.note = NotesService.getNote($stateParams.id);
44+
} else {
45+
$scope.note = {};
46+
}
47+
48+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
var app = angular.module('com.module.notes');
3+
4+
app.service('NotesService', ['$state', 'CoreService', 'Note', function ($state, CoreService, Note) {
5+
6+
this.getNotes = function () {
7+
return Note.find();
8+
};
9+
10+
this.getNote = function (id) {
11+
return Note.findById({
12+
id: id
13+
});
14+
};
15+
16+
this.upsertNote = function (note, cb) {
17+
Note.upsert(note, function () {
18+
CoreService.toastSuccess('Note saved', 'Your note is safe with us!');
19+
cb();
20+
}, function (err) {
21+
CoreService.toastSuccess('Error saving note ', 'This note could no be saved: ' + err);
22+
});
23+
};
24+
25+
this.deleteNote = function (id, cb) {
26+
CoreService.confirm('Are you sure?', 'Deleting this cannot be undone', function () {
27+
Note.deleteById(id, function () {
28+
CoreService.toastSuccess('Note deleted', 'Your note is deleted!');
29+
cb();
30+
}, function (err) {
31+
CoreService.toastError('Error deleting note', 'Your note is not deleted! ' + err);
32+
});
33+
}, function () {
34+
return false;
35+
});
36+
};
37+
38+
}]);

0 commit comments

Comments
 (0)