From 1a783ba4f8653b3fa2b90b7432ad58053c52f29f Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 15 Sep 2015 16:11:10 -0400 Subject: [PATCH] feat(client:main): use a class for the main controller --- .../client/app/main/main.controller.js | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/app/templates/client/app/main/main.controller.js b/app/templates/client/app/main/main.controller.js index db9bbd5c3..f69ad1e8b 100644 --- a/app/templates/client/app/main/main.controller.js +++ b/app/templates/client/app/main/main.controller.js @@ -2,30 +2,32 @@ (function() { -function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { - var self = this; - this.awesomeThings = []; - - $http.get('/api/things').then(function(response) { - self.awesomeThings = response.data;<% if (filters.socketio) { %> - socket.syncUpdates('thing', self.awesomeThings);<% } %> - });<% if (filters.models) { %> - - this.addThing = function() { - if (self.newThing === '') { - return; +class MainController { + + constructor($http<% if (filters.socketio) { %>, $scope, socket<% } %>) { + this.$http = $http; + this.awesomeThings = []; + + $http.get('/api/things').then(response => { + this.awesomeThings = response.data;<% if (filters.socketio) { %> + socket.syncUpdates('thing', this.awesomeThings);<% } %> + });<% if (filters.socketio) { %> + + $scope.$on('$destroy', function() { + socket.unsyncUpdates('thing'); + });<% } %> + }<% if (filters.models) { %> + + addThing() { + if (this.newThing) { + this.$http.post('/api/things', { name: this.newThing }); + this.newThing = ''; } - $http.post('/api/things', { name: self.newThing }); - self.newThing = ''; - }; + } - this.deleteThing = function(thing) { - $http.delete('/api/things/' + thing._id); - };<% } if (filters.socketio) { %> - - $scope.$on('$destroy', function() { - socket.unsyncUpdates('thing'); - });<% } %> + deleteThing(thing) { + this.$http.delete('/api/things/' + thing._id); + }<% } %> } angular.module('<%= scriptAppName %>')