Skip to content

Commit a5de41d

Browse files
committed
feat(client:main): use a class for the main controller
1 parent 97d4d6f commit a5de41d

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

Diff for: app/templates/client/app/main/main.controller.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
'use strict';
22
(function() {
33

4-
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
5-
var self = this;
6-
this.awesomeThings = [];
7-
8-
$http.get('/api/things').then(function(response) {
9-
self.awesomeThings = response.data;<% if (filters.socketio) { %>
10-
socket.syncUpdates('thing', self.awesomeThings);<% } %>
11-
});
12-
<% if (filters.models) { %>
13-
this.addThing = function() {
14-
if (self.newThing === '') {
4+
class MainController {
5+
awesomeThings = [];
6+
7+
constructor($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
8+
this.$http = $http;
9+
10+
$http.get('/api/things').then(response => {
11+
this.awesomeThings = response.data;<% if (filters.socketio) { %>
12+
socket.syncUpdates('thing', this.awesomeThings);<% } %>
13+
});
14+
15+
$scope.$on('$destroy', function() {
16+
socket.unsyncUpdates('thing');
17+
});<% } %>
18+
}
19+
20+
<% if (filters.models) { %>
21+
addThing() {
22+
if (this.newThing === '') {
1523
return;
1624
}
17-
$http.post('/api/things', { name: self.newThing });
18-
self.newThing = '';
25+
this.$http.post('/api/things', { name: this.newThing });
26+
this.newThing = '';
1927
};
2028

21-
this.deleteThing = function(thing) {
22-
$http.delete('/api/things/' + thing._id);
29+
deleteThing(thing) {
30+
this.$http.delete('/api/things/' + thing._id);
2331
};<% } %><% if (filters.socketio) { %>
24-
25-
$scope.$on('$destroy', function() {
26-
socket.unsyncUpdates('thing');
27-
});<% } %>
2832
}
2933

3034
angular.module('<%= scriptAppName %>')

0 commit comments

Comments
 (0)