Skip to content

Commit ff51c05

Browse files
committed
refactor(client:main): use controller as, named function, new name
controller as main, MainCtrl -> MainController
1 parent b7c613e commit ff51c05

8 files changed

+77
-62
lines changed

Diff for: app/templates/client/app/main/main(coffee).coffee

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ angular.module '<%= scriptAppName %>'
55
$routeProvider
66
.when '/',
77
templateUrl: 'app/main/main.html'
8-
controller: 'MainCtrl'
8+
controller: 'MainController'
9+
controllerAs: 'main'
910
<% } %><% if (filters.uirouter) { %>.config ($stateProvider) ->
1011
$stateProvider
1112
.state 'main',
1213
url: '/'
1314
templateUrl: 'app/main/main.html'
14-
controller: 'MainCtrl'
15+
controller: 'MainController'
16+
controllerAs: 'main'
1517
<% } %>

Diff for: app/templates/client/app/main/main(html).html

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ <h1>'Allo, 'Allo!</h1>
1212
<div class="row">
1313
<div class="col-lg-12">
1414
<h1 class="page-header">Features:</h1>
15-
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in awesomeThings">
16-
<li><a href="#" tooltip="{{thing.info}}">{{thing.name}}<% if (filters.socketio) { %><button type="button" class="close" ng-click="deleteThing(thing)">&times;</button><% } %></a></li>
15+
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in main.awesomeThings">
16+
<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>
1717
</ul>
1818
</div>
1919
</div><% if (filters.socketio) { %>
2020

2121
<form class="thing-form">
2222
<label>Syncs in realtime across clients</label>
2323
<p class="input-group">
24-
<input type="text" class="form-control" placeholder="Add a new thing here." ng-model="newThing">
24+
<input type="text" class="form-control" placeholder="Add a new thing here." ng-model="main.newThing">
2525
<span class="input-group-btn">
26-
<button type="submit" class="btn btn-primary" ng-click="addThing()">Add New</button>
26+
<button type="submit" class="btn btn-primary" ng-click="main.addThing()">Add New</button>
2727
</span>
2828
</p>
2929
</form><% } %>

Diff for: app/templates/client/app/main/main(jade).jade

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ header#banner.hero-unit
1010
.row
1111
.col-lg-12
1212
h1.page-header Features:
13-
ul.nav.nav-tabs.nav-stacked.col-md-4.col-lg-4.col-sm-6(ng-repeat='thing in awesomeThings')
13+
ul.nav.nav-tabs.nav-stacked.col-md-4.col-lg-4.col-sm-6(ng-repeat='thing in main.awesomeThings')
1414
li
1515
a(href='#', tooltip='{{thing.info}}')
1616
| {{thing.name}}<% if (filters.socketio) { %>
17-
button.close(type='button', ng-click='deleteThing(thing)') &times;<% } %><% if (filters.socketio) { %>
17+
button.close(type='button', ng-click='main.deleteThing(thing)') &times;<% } %><% if (filters.socketio) { %>
1818

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

2626
footer

Diff for: app/templates/client/app/main/main(js).js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ angular.module('<%= scriptAppName %>')
55
$routeProvider
66
.when('/', {
77
templateUrl: 'app/main/main.html',
8-
controller: 'MainCtrl'
8+
controller: 'MainController',
9+
conterollerAs: 'main'
910
});
1011
});<% } %><% if (filters.uirouter) { %>.config(function($stateProvider) {
1112
$stateProvider
1213
.state('main', {
1314
url: '/',
1415
templateUrl: 'app/main/main.html',
15-
controller: 'MainCtrl'
16+
controller: 'MainController',
17+
conterollerAs: 'main'
1618
});
1719
});<% } %>
+21-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
'use strict'
22

3-
angular.module '<%= scriptAppName %>'
4-
.controller 'MainCtrl', ($scope, $http<% if (filters.socketio) { %>, socket<% } %>) ->
5-
$scope.awesomeThings = []
6-
7-
$http.get('/api/things').then (response) ->
8-
$scope.awesomeThings = response.data
9-
<% if (filters.socketio) { %>socket.syncUpdates 'thing', $scope.awesomeThings<% } %>
3+
MainController = ($scope, $http<% if (filters.socketio) { %>, socket<% } %>) ->
4+
@awesomeThings = []
5+
$http.get('/api/things').then (response) =>
6+
this.awesomeThings = response.data<% if (filters.socketio) { %>
7+
socket.syncUpdates 'thing', this.awesomeThings<% } %>
8+
return
109
<% if (filters.models) { %>
11-
$scope.addThing = ->
12-
return if $scope.newThing is ''
13-
$http.post '/api/things',
14-
name: $scope.newThing
15-
16-
$scope.newThing = ''
10+
@addThing = =>
11+
if this.newThing == ''
12+
return
13+
$http.post '/api/things', name: this.newThing
14+
this.newThing = ''
15+
return
1716

18-
$scope.deleteThing = (thing) ->
19-
$http.delete '/api/things/' + thing._id<% } %><% if (filters.socketio) { %>
17+
@deleteThing = (thing) ->
18+
$http.delete '/api/things/' + thing._id
19+
return<% } %><% if (filters.socketio) { %>
2020

2121
$scope.$on '$destroy', ->
22-
socket.unsyncUpdates 'thing'<% } %>
22+
socket.unsyncUpdates 'thing'
23+
return<% } %>
24+
return this
25+
26+
angular.module '<%= scriptAppName %>'
27+
.controller 'MainController', MainController

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

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
'use strict';
2+
(function() {
23

3-
angular.module('<%= scriptAppName %>')
4-
.controller('MainCtrl', function($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
5-
$scope.awesomeThings = [];
4+
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
5+
var self = this;
6+
this.awesomeThings = [];
67

7-
$http.get('/api/things').then(function(response) {
8-
$scope.awesomeThings = response.data;<% if (filters.socketio) { %>
9-
socket.syncUpdates('thing', $scope.awesomeThings);<% } %>
10-
});
8+
$http.get('/api/things').then(function(response) {
9+
self.awesomeThings = response.data;<% if (filters.socketio) { %>
10+
socket.syncUpdates('thing', self.awesomeThings);<% } %>
11+
});
1112
<% if (filters.models) { %>
12-
$scope.addThing = function() {
13-
if ($scope.newThing === '') {
14-
return;
15-
}
16-
$http.post('/api/things', { name: $scope.newThing });
17-
$scope.newThing = '';
18-
};
13+
this.addThing = function() {
14+
if (self.newThing === '') {
15+
return;
16+
}
17+
$http.post('/api/things', { name: self.newThing });
18+
self.newThing = '';
19+
};
1920

20-
$scope.deleteThing = function(thing) {
21-
$http.delete('/api/things/' + thing._id);
22-
};<% } %><% if (filters.socketio) { %>
21+
this.deleteThing = function(thing) {
22+
$http.delete('/api/things/' + thing._id);
23+
};<% } %><% if (filters.socketio) { %>
2324

24-
$scope.$on('$destroy', function() {
25-
socket.unsyncUpdates('thing');
26-
});<% } %>
27-
});
25+
$scope.$on('$destroy', function() {
26+
socket.unsyncUpdates('thing');
27+
});<% } %>
28+
}
29+
30+
angular.module('<%= scriptAppName %>')
31+
.controller('MainController', MainController);
32+
33+
})();

Diff for: app/templates/client/app/main/main.controller.spec(coffee).coffee

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict'
22

3-
describe 'Controller: MainCtrl', ->
3+
describe 'Controller: MainController', ->
44

55
# load the controller's module
66
beforeEach module '<%= scriptAppName %>' <% if (filters.uirouter) {%>
77
beforeEach module 'stateMock' <% } %><% if (filters.socketio) {%>
88
beforeEach module 'socketMock' <% } %>
99

10-
MainCtrl = undefined
10+
MainController = undefined
1111
scope = undefined<% if (filters.uirouter) {%>
1212
state = undefined<% } %>
1313
$httpBackend = undefined
@@ -23,10 +23,10 @@ describe 'Controller: MainCtrl', ->
2323
]
2424
scope = $rootScope.$new()<% if (filters.uirouter) {%>
2525
state = $state<% } %>
26-
MainCtrl = $controller 'MainCtrl',
26+
MainController = $controller 'MainController',
2727
$scope: scope
2828

29-
it 'should attach a list of things to the scope', ->
29+
it 'should attach a list of things to the controller', ->
3030
$httpBackend.flush()<% if (filters.jasmine) { %>
31-
expect(scope.awesomeThings.length).toBe 4 <% } if (filters.mocha) { %>
32-
<%= expect() %>scope.awesomeThings.length<%= to() %>.equal 4<% } %>
31+
expect(MainController.awesomeThings.length).toBe 4 <% } if (filters.mocha) { %>
32+
<%= expect() %>MainController.awesomeThings.length<%= to() %>.equal 4<% } %>
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

3-
describe('Controller: MainCtrl', function() {
3+
describe('Controller: MainController', function() {
44

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

10-
var MainCtrl;
11-
var scope;<% if (filters.uirouter) {%>
10+
var scope;
11+
var MainController;<% if (filters.uirouter) {%>
1212
var state;<% } %>
1313
var $httpBackend;
1414

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

2121
scope = $rootScope.$new();<% if (filters.uirouter) {%>
2222
state = $state;<% } %>
23-
MainCtrl = $controller('MainCtrl', {
23+
MainController = $controller('MainController', {
2424
$scope: scope
2525
});
2626
}));
2727

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

0 commit comments

Comments
 (0)