Skip to content

Commit 3e901b0

Browse files
committed
Merge pull request #1314 from DaftMonk/main-controller-class
Main controller as class
2 parents 10b5179 + 1a783ba commit 3e901b0

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

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

+24-22
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
(function() {
44

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

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

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

0 commit comments

Comments
 (0)