Skip to content

Commit fade652

Browse files
committed
Merge pull request angular-fullstack#1365 from DaftMonk/more-controller-as
More controller as
2 parents c0153d4 + 02773df commit fade652

File tree

19 files changed

+254
-201
lines changed

19 files changed

+254
-201
lines changed

app/templates/Gruntfile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,10 @@ module.exports = function (grunt) {
589589
// Compiles ES6 to JavaScript using Babel
590590
babel: {
591591
options: {
592-
sourceMap: true
592+
sourceMap: true,
593+
optional: [
594+
'es7.classProperties'
595+
]
593596
},<% if(filters.babel) { %>
594597
client: {
595598
files: [{

app/templates/client/.jshintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"strict": true,
1717
"trailing": true,
1818
"smarttabs": true,
19+
"ignoreDelimiters": [
20+
{ "start": "start-non-standard", "end": "end-non-standard" }
21+
],
1922
"globals": {
2023
"jQuery": true,
2124
"angular": true,

app/templates/client/app/account(auth)/account.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ angular.module('<%= scriptAppName %>')
55
$routeProvider
66
.when('/login', {
77
templateUrl: 'app/account/login/login.html',
8-
controller: 'LoginCtrl'
8+
controller: 'LoginController',
9+
controllerAs: 'vm'
910
})
1011
.when('/logout', {
1112
name: 'logout',
@@ -21,11 +22,13 @@ angular.module('<%= scriptAppName %>')
2122
})
2223
.when('/signup', {
2324
templateUrl: 'app/account/signup/signup.html',
24-
controller: 'SignupCtrl'
25+
controller: 'SignupController',
26+
controllerAs: 'vm'
2527
})
2628
.when('/settings', {
2729
templateUrl: 'app/account/settings/settings.html',
28-
controller: 'SettingsCtrl',
30+
controller: 'SettingsController',
31+
controllerAs: 'vm',
2932
authenticate: true
3033
});
3134
})
@@ -40,7 +43,8 @@ angular.module('<%= scriptAppName %>')
4043
.state('login', {
4144
url: '/login',
4245
templateUrl: 'app/account/login/login.html',
43-
controller: 'LoginCtrl'
46+
controller: 'LoginController',
47+
controllerAs: 'vm'
4448
})
4549
.state('logout', {
4650
url: '/logout?referrer',
@@ -57,12 +61,14 @@ angular.module('<%= scriptAppName %>')
5761
.state('signup', {
5862
url: '/signup',
5963
templateUrl: 'app/account/signup/signup.html',
60-
controller: 'SignupCtrl'
64+
controller: 'SignupController',
65+
controllerAs: 'vm'
6166
})
6267
.state('settings', {
6368
url: '/settings',
6469
templateUrl: 'app/account/settings/settings.html',
65-
controller: 'SettingsCtrl',
70+
controller: 'SettingsController',
71+
controllerAs: 'vm',
6672
authenticate: true
6773
});
6874
})

app/templates/client/app/account(auth)/login/login(html).html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ <h1>Login</h1>
88
<p>Admin account is <code>[email protected]</code> / <code>admin</code></p>
99
</div>
1010
<div class="col-sm-12">
11-
<form class="form" name="form" ng-submit="login(form)" novalidate>
11+
<form class="form" name="form" ng-submit="vm.login(form)" novalidate>
1212

1313
<div class="form-group">
1414
<label>Email</label>
1515

16-
<input type="email" name="email" class="form-control" ng-model="user.email" required>
16+
<input type="email" name="email" class="form-control" ng-model="vm.user.email" required>
1717
</div>
1818

1919
<div class="form-group">
2020
<label>Password</label>
2121

22-
<input type="password" name="password" class="form-control" ng-model="user.password" required>
22+
<input type="password" name="password" class="form-control" ng-model="vm.user.password" required>
2323
</div>
2424

2525
<div class="form-group has-error">
26-
<p class="help-block" ng-show="form.email.$error.required && form.password.$error.required && submitted">
26+
<p class="help-block" ng-show="form.email.$error.required && form.password.$error.required && vm.submitted">
2727
Please enter your email and password.
2828
</p>
29-
<p class="help-block" ng-show="form.email.$error.email && submitted">
29+
<p class="help-block" ng-show="form.email.$error.email && vm.submitted">
3030
Please enter a valid email.
3131
</p>
3232

33-
<p class="help-block">{{ errors.other }}</p>
33+
<p class="help-block">{{ vm.errors.other }}</p>
3434
</div>
3535

3636
<div>

app/templates/client/app/account(auth)/login/login(jade).jade

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ navbar
1717
code admin
1818

1919
.col-sm-12
20-
form.form(name='form', ng-submit='login(form)', novalidate='')
20+
form.form(name='form', ng-submit='vm.login(form)', novalidate='')
2121
.form-group
2222
label Email
23-
input.form-control(type='email', name='email', ng-model='user.email')
23+
input.form-control(type='email', name='email', ng-model='vm.user.email')
2424
.form-group
2525
label Password
26-
input.form-control(type='password', name='password', ng-model='user.password')
26+
input.form-control(type='password', name='password', ng-model='vm.user.password')
2727

2828
.form-group.has-error
29-
p.help-block(ng-show='form.email.$error.required && form.password.$error.required && submitted')
29+
p.help-block(ng-show='form.email.$error.required && form.password.$error.required && vm.submitted')
3030
| Please enter your email and password.
31-
p.help-block {{ errors.other }}
31+
p.help-block {{ vm.errors.other }}
3232

3333
div
3434
button.btn.btn-inverse.btn-lg.btn-login(type='submit')
Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
'use strict';
22

3-
angular.module('<%= scriptAppName %>')
4-
.controller('LoginCtrl', function($scope, Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
5-
$scope.user = {};
6-
$scope.errors = {};
3+
class LoginController {
4+
//start-non-standard
5+
user = {};
6+
errors = {};
7+
submitted = false;
8+
//end-non-standard
9+
10+
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
11+
this.Auth = Auth;<% if (filters.ngroute) { %>
12+
this.$location = $location;<% } if (filters.uirouter) { %>
13+
this.$state = $state;<% } %>
14+
}
715

8-
$scope.login = function(form) {
9-
$scope.submitted = true;
16+
login(form) {
17+
this.submitted = true;
1018

11-
if (form.$valid) {
12-
Auth.login({
13-
email: $scope.user.email,
14-
password: $scope.user.password
15-
})
16-
.then(function() {
17-
// Logged in, redirect to home
18-
<% if (filters.ngroute) { %>$location.path('/');<% } %><% if (filters.uirouter) { %>$state.go('main');<% } %>
19-
})
20-
.catch(function(err) {
21-
$scope.errors.other = err.message;
22-
});
23-
}
24-
};
19+
if (form.$valid) {
20+
this.Auth.login({
21+
email: this.user.email,
22+
password: this.user.password
23+
})
24+
.then(() => {
25+
// Logged in, redirect to home
26+
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
27+
})
28+
.catch(err => {
29+
this.errors.other = err.message;
30+
});
31+
}
32+
}
33+
}
2534

26-
});
35+
angular.module('<%= scriptAppName %>')
36+
.controller('LoginController', LoginController);

app/templates/client/app/account(auth)/settings/settings(html).html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@
66
<h1>Change Password</h1>
77
</div>
88
<div class="col-sm-12">
9-
<form class="form" name="form" ng-submit="changePassword(form)" novalidate>
9+
<form class="form" name="form" ng-submit="vm.changePassword(form)" novalidate>
1010

1111
<div class="form-group">
1212
<label>Current Password</label>
1313

14-
<input type="password" name="password" class="form-control" ng-model="user.oldPassword"
14+
<input type="password" name="password" class="form-control" ng-model="vm.user.oldPassword"
1515
mongoose-error/>
1616
<p class="help-block" ng-show="form.password.$error.mongoose">
17-
{{ errors.other }}
17+
{{ vm.errors.other }}
1818
</p>
1919
</div>
2020

2121
<div class="form-group">
2222
<label>New Password</label>
2323

24-
<input type="password" name="newPassword" class="form-control" ng-model="user.newPassword"
24+
<input type="password" name="newPassword" class="form-control" ng-model="vm.user.newPassword"
2525
ng-minlength="3"
2626
required/>
2727
<p class="help-block"
28-
ng-show="(form.newPassword.$error.minlength || form.newPassword.$error.required) && (form.newPassword.$dirty || submitted)">
28+
ng-show="(form.newPassword.$error.minlength || form.newPassword.$error.required) && (form.newPassword.$dirty || vm.submitted)">
2929
Password must be at least 3 characters.
3030
</p>
3131
</div>
3232

3333
<div class="form-group">
3434
<label>Confirm New Password</label>
3535

36-
<input type="password" name="confirmPassword" class="form-control" ng-model="user.confirmPassword"
37-
match="user.newPassword"
36+
<input type="password" name="confirmPassword" class="form-control" ng-model="vm.user.confirmPassword"
37+
match="vm.user.newPassword"
3838
ng-minlength="3"
3939
required=""/>
4040
<p class="help-block"
41-
ng-show="form.confirmPassword.$error.match && submitted">
41+
ng-show="form.confirmPassword.$error.match && vm.submitted">
4242
Passwords must match.
4343
</p>
4444

4545
</div>
4646

47-
<p class="help-block"> {{ message }} </p>
47+
<p class="help-block"> {{ vm.message }} </p>
4848

4949
<button class="btn btn-lg btn-primary" type="submit">Save changes</button>
5050
</form>

app/templates/client/app/account(auth)/settings/settings(jade).jade

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ navbar
44
.col-sm-12
55
h1 Change Password
66
.col-sm-12
7-
form.form(name='form', ng-submit='changePassword(form)', novalidate='')
7+
form.form(name='form', ng-submit='vm.changePassword(form)', novalidate='')
88
.form-group
99
label Current Password
1010
input.form-control(type='password'
1111
name='password'
12-
ng-model='user.oldPassword'
12+
ng-model='vm.user.oldPassword'
1313
mongoose-error='')
1414
p.help-block(ng-show='form.password.$error.mongoose')
15-
| {{ errors.other }}
15+
| {{ vm.errors.other }}
1616
.form-group
1717
label New Password
1818
input.form-control(type='password'
1919
name='newPassword'
20-
ng-model='user.newPassword'
20+
ng-model='vm.user.newPassword'
2121
ng-minlength='3', required='')
22-
p.help-block(ng-show='(form.newPassword.$error.minlength || form.newPassword.$error.required) && (form.newPassword.$dirty || submitted)')
22+
p.help-block(ng-show='(form.newPassword.$error.minlength || form.newPassword.$error.required) && (form.newPassword.$dirty || vm.submitted)')
2323
| Password must be at least 3 characters.
2424
.form-group
2525
label Confirm New Password
2626
input.form-control(type='password'
2727
name='confirmPassword'
28-
ng-model='user.confirmPassword'
29-
match="user.newPassword"
28+
ng-model='vm.user.confirmPassword'
29+
match="vm.user.newPassword"
3030
ng-minlength='3', required='')
31-
p.help-block(ng-show='form.confirmPassword.$error.match && submitted')
31+
p.help-block(ng-show='fvm.orm.confirmPassword.$error.match && vm.submitted')
3232
| Passwords must match.
3333

34-
p.help-block {{ message }}
34+
p.help-block {{ vm.message }}
3535

3636
button.btn.btn-lg.btn-primary(type='submit') Save changes
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
'use strict';
22

3-
angular.module('<%= scriptAppName %>')
4-
.controller('SettingsCtrl', function($scope, User, Auth) {
5-
$scope.errors = {};
3+
class SettingsController {
4+
//start-non-standard
5+
errors = {};
6+
submitted = false;
7+
//end-non-standard
8+
9+
constructor(Auth) {
10+
this.Auth = Auth;
11+
}
12+
13+
changePassword(form) {
14+
this.submitted = true;
615

7-
$scope.changePassword = function(form) {
8-
$scope.submitted = true;
9-
if (form.$valid) {
10-
Auth.changePassword($scope.user.oldPassword, $scope.user.newPassword)
11-
.then(function() {
12-
$scope.message = 'Password successfully changed.';
13-
})
14-
.catch(function() {
15-
form.password.$setValidity('mongoose', false);
16-
$scope.errors.other = 'Incorrect password';
17-
$scope.message = '';
18-
});
19-
}
20-
};
21-
});
16+
if (form.$valid) {
17+
this.Auth.changePassword(this.user.oldPassword, this.user.newPassword)
18+
.then(() => {
19+
this.message = 'Password successfully changed.';
20+
})
21+
.catch(() => {
22+
form.password.$setValidity('mongoose', false);
23+
this.errors.other = 'Incorrect password';
24+
this.message = '';
25+
});
26+
}
27+
}
28+
}
29+
30+
angular.module('<%= scriptAppName %>')
31+
.controller('SettingsController', SettingsController);

0 commit comments

Comments
 (0)