Skip to content

Commit c8038ce

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

File tree

5 files changed

+46
-38
lines changed

5 files changed

+46
-38
lines changed

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
});<% } %>

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+
})();
+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)