Skip to content

Commit d3dc04a

Browse files
Marco ChávezAwk34
Marco Chávez
authored andcommitted
chore(main): use component in templates/client/app/main
1 parent 5064a6a commit d3dc04a

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ <h1>'Allo, 'Allo!</h1>
1010
<div class="row">
1111
<div class="col-lg-12">
1212
<h1 class="page-header">Features:</h1>
13-
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in main.awesomeThings">
14-
<li><a href="#" uib-tooltip="{{thing.info}}">{{thing.name}}<% if (filters.socketio) { %><button type="button" class="close" ng-click="main.deleteThing(thing)">&times;</button><% } %></a></li>
13+
<ul class="nav nav-tabs nav-stacked col-md-4 col-lg-4 col-sm-6" ng-repeat="thing in $ctrl.awesomeThings">
14+
<li><a href="#" uib-tooltip="{{thing.info}}">{{thing.name}}<% if (filters.socketio) { %><button type="button" class="close" ng-click="$ctrl.deleteThing(thing)">&times;</button><% } %></a></li>
1515
</ul>
1616
</div>
1717
</div><% if (filters.socketio) { %>
1818

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

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

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

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

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
class MainController {
66

77
constructor($http<% if (filters.socketio) { %>, $scope, socket<% } %>) {
8-
this.$http = $http;
9-
this.awesomeThings = [];
10-
11-
$http.get('/api/things').then(response => {
12-
this.awesomeThings = response.data;<% if (filters.socketio) { %>
13-
socket.syncUpdates('thing', this.awesomeThings);<% } %>
14-
});<% if (filters.socketio) { %>
8+
this.$http = $http;<% if (filters.socketio) { %>
9+
this.socket = socket;<% } %>
10+
this.awesomeThings = [];<% if (filters.socketio) { %>
1511

1612
$scope.$on('$destroy', function() {
1713
socket.unsyncUpdates('thing');
1814
});<% } %>
15+
}
16+
17+
$onInit() {
18+
this.$http.get('/api/things').then(response => {
19+
this.awesomeThings = response.data;<% if (filters.socketio) { %>
20+
this.socket.syncUpdates('thing', this.awesomeThings);<% } %>
21+
});
1922
}<% if (filters.models) { %>
2023

2124
addThing() {
@@ -31,6 +34,9 @@ class MainController {
3134
}
3235

3336
angular.module('<%= scriptAppName %>')
34-
.controller('MainController', MainController);
37+
.component('main', {
38+
templateUrl: 'app/main/main.html',
39+
controller: MainController
40+
});
3541

3642
})();
+10-7
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
'use strict';
22

3-
describe('Controller: MainController', function() {
3+
describe('Component: mainComponent', 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

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

1515
// Initialize the controller and a mock scope
16-
beforeEach(inject(function(_$httpBackend_, $controller, $rootScope<% if (filters.uirouter) {%>, $state<% } %>) {
16+
beforeEach(inject(function(_$httpBackend_, $http, $componentController, $rootScope<% if (filters.uirouter) {%>, $state<% } %><% if (filters.socketio) {%>, socket<% } %>) {
1717
$httpBackend = _$httpBackend_;
1818
$httpBackend.expectGET('/api/things')
1919
.respond(['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express']);
2020

2121
scope = $rootScope.$new();<% if (filters.uirouter) {%>
2222
state = $state;<% } %>
23-
MainController = $controller('MainController', {
24-
$scope: scope
23+
mainComponent = $componentController('main', {
24+
$http: $http,
25+
$scope: scope<% if (filters.socketio) {%>,
26+
socket: socket<% } %>
2527
});
2628
}));
2729

2830
it('should attach a list of things to the controller', function() {
31+
mainComponent.$onInit();
2932
$httpBackend.flush();<% if (filters.jasmine) { %>
30-
expect(MainController.awesomeThings.length).toBe(4);<% } if (filters.mocha) { %>
31-
<%= expect() %>MainController.awesomeThings.length<%= to() %>.equal(4);<% } %>
33+
expect(mainComponent.awesomeThings.length).toBe(4);<% } if (filters.mocha) { %>
34+
<%= expect() %>mainComponent.awesomeThings.length<%= to() %>.equal(4);<% } %>
3235
});
3336
});

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ angular.module('<%= scriptAppName %>')
44
<% if (filters.ngroute) { %>.config(function($routeProvider) {
55
$routeProvider
66
.when('/', {
7-
templateUrl: 'app/main/main.html',
8-
controller: 'MainController',
9-
controllerAs: 'main'
7+
template: '<main></main>'
108
});
119
});<% } %><% if (filters.uirouter) { %>.config(function($stateProvider) {
1210
$stateProvider
1311
.state('main', {
1412
url: '/',
15-
templateUrl: 'app/main/main.html',
16-
controller: 'MainController',
17-
controllerAs: 'main'
13+
template: '<main></main>'
1814
});
1915
});<% } %>

0 commit comments

Comments
 (0)