-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(client:main): use controller as, named function, new name #1243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
'use strict'; | ||
(function() { | ||
|
||
angular.module('<%= scriptAppName %>') | ||
.controller('MainCtrl', function($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { | ||
$scope.awesomeThings = []; | ||
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { | ||
var self = this; | ||
this.awesomeThings = []; | ||
|
||
$http.get('/api/things').then(function(response) { | ||
$scope.awesomeThings = response.data;<% if (filters.socketio) { %> | ||
socket.syncUpdates('thing', $scope.awesomeThings);<% } %> | ||
}); | ||
$http.get('/api/things').then(function(response) { | ||
self.awesomeThings = response.data;<% if (filters.socketio) { %> | ||
socket.syncUpdates('thing', self.awesomeThings);<% } %> | ||
}); | ||
<% if (filters.models) { %> | ||
$scope.addThing = function() { | ||
if ($scope.newThing === '') { | ||
return; | ||
} | ||
$http.post('/api/things', { name: $scope.newThing }); | ||
$scope.newThing = ''; | ||
}; | ||
this.addThing = function() { | ||
if (self.newThing === '') { | ||
return; | ||
} | ||
$http.post('/api/things', { name: self.newThing }); | ||
self.newThing = ''; | ||
}; | ||
|
||
$scope.deleteThing = function(thing) { | ||
$http.delete('/api/things/' + thing._id); | ||
};<% } %><% if (filters.socketio) { %> | ||
this.deleteThing = function(thing) { | ||
$http.delete('/api/things/' + thing._id); | ||
};<% } %><% if (filters.socketio) { %> | ||
|
||
$scope.$on('$destroy', function() { | ||
socket.unsyncUpdates('thing'); | ||
});<% } %> | ||
}); | ||
$scope.$on('$destroy', function() { | ||
socket.unsyncUpdates('thing'); | ||
});<% } %> | ||
} | ||
|
||
angular.module('<%= scriptAppName %>') | ||
.controller('MainController', MainController); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since function declarations are hoisted, we can initialize the controller at the top of the file which makes it more readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put the function before, since we'll want to use classes with es2015 code, and classes aren't hoisted. Consistency. |
||
|
||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use IIFE's instead of declaring functions globally.