Skip to content

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

Merged
merged 1 commit into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/templates/client/app/main/main(html).html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ <h1>'Allo, 'Allo!</h1>
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Features:</h1>
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in awesomeThings">
<li><a href="#" tooltip="{{thing.info}}">{{thing.name}}<% if (filters.socketio) { %><button type="button" class="close" ng-click="deleteThing(thing)">&times;</button><% } %></a></li>
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in main.awesomeThings">
<li><a href="#" tooltip="{{thing.info}}">{{thing.name}}<% if (filters.socketio) { %><button type="button" class="close" ng-click="main.deleteThing(thing)">&times;</button><% } %></a></li>
</ul>
</div>
</div><% if (filters.socketio) { %>

<form class="thing-form">
<label>Syncs in realtime across clients</label>
<p class="input-group">
<input type="text" class="form-control" placeholder="Add a new thing here." ng-model="newThing">
<input type="text" class="form-control" placeholder="Add a new thing here." ng-model="main.newThing">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary" ng-click="addThing()">Add New</button>
<button type="submit" class="btn btn-primary" ng-click="main.addThing()">Add New</button>
</span>
</p>
</form><% } %>
Expand Down
8 changes: 4 additions & 4 deletions app/templates/client/app/main/main(jade).jade
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ header#banner.hero-unit
.row
.col-lg-12
h1.page-header Features:
ul.nav.nav-tabs.nav-stacked.col-md-4.col-lg-4.col-sm-6(ng-repeat='thing in awesomeThings')
ul.nav.nav-tabs.nav-stacked.col-md-4.col-lg-4.col-sm-6(ng-repeat='thing in main.awesomeThings')
li
a(href='#', tooltip='{{thing.info}}')
| {{thing.name}}<% if (filters.socketio) { %>
button.close(type='button', ng-click='deleteThing(thing)') &times;<% } %><% if (filters.socketio) { %>
button.close(type='button', ng-click='main.deleteThing(thing)') &times;<% } %><% if (filters.socketio) { %>

form.thing-form
label Syncs in realtime across clients
p.input-group
input.form-control(type='text', placeholder='Add a new thing here.', ng-model='newThing')
input.form-control(type='text', placeholder='Add a new thing here.', ng-model='main.newThing')
span.input-group-btn
button.btn.btn-primary(type='submit', ng-click='addThing()') Add New<% } %>
button.btn.btn-primary(type='submit', ng-click='main.addThing()') Add New<% } %>

footer
6 changes: 4 additions & 2 deletions app/templates/client/app/main/main(js).js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ angular.module('<%= scriptAppName %>')
$routeProvider
.when('/', {
templateUrl: 'app/main/main.html',
controller: 'MainCtrl'
controller: 'MainController',
conterollerAs: 'main'
});
});<% } %><% if (filters.uirouter) { %>.config(function($stateProvider) {
$stateProvider
.state('main', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainCtrl'
controller: 'MainController',
conterollerAs: 'main'
});
});<% } %>
48 changes: 27 additions & 21 deletions app/templates/client/app/main/main.controller(js).js
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<% } %>) {
Copy link
Member

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.

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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.


})();
14 changes: 7 additions & 7 deletions app/templates/client/app/main/main.controller.spec(js).js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

describe('Controller: MainCtrl', function() {
describe('Controller: MainController', function() {

// load the controller's module
beforeEach(module('<%= scriptAppName %>'));<% if (filters.uirouter) {%>
beforeEach(module('stateMock'));<% } %><% if (filters.socketio) {%>
beforeEach(module('socketMock'));<% } %>

var MainCtrl;
var scope;<% if (filters.uirouter) {%>
var scope;
var MainController;<% if (filters.uirouter) {%>
var state;<% } %>
var $httpBackend;

Expand All @@ -20,14 +20,14 @@ describe('Controller: MainCtrl', function() {

scope = $rootScope.$new();<% if (filters.uirouter) {%>
state = $state;<% } %>
MainCtrl = $controller('MainCtrl', {
MainController = $controller('MainController', {
$scope: scope
});
}));

it('should attach a list of things to the scope', function() {
it('should attach a list of things to the controller', function() {
$httpBackend.flush();<% if (filters.jasmine) { %>
expect(scope.awesomeThings.length).toBe(4);<% } if (filters.mocha) { %>
<%= expect() %>scope.awesomeThings.length<%= to() %>.equal(4);<% } %>
expect(MainController.awesomeThings.length).toBe(4);<% } if (filters.mocha) { %>
<%= expect() %>MainController.awesomeThings.length<%= to() %>.equal(4);<% } %>
});
});