Skip to content

Commit 326c69b

Browse files
committed
refactor(client:account): use controller as and classes
1 parent 519fd36 commit 326c69b

File tree

12 files changed

+188
-157
lines changed

12 files changed

+188
-157
lines changed

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: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
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+
user = {};
5+
errors = {};
6+
submitted = false;
7+
8+
constructor(Auth<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>) {
9+
this.Auth = Auth;<% if (filters.ngroute) { %>
10+
this.$location = $location;<% } if (filters.uirouter) { %>
11+
this.$state = $state;<% } %>
12+
}
713

8-
$scope.login = function(form) {
9-
$scope.submitted = true;
14+
login(form) {
15+
this.submitted = true;
1016

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-
};
17+
if (form.$valid) {
18+
this.Auth.login({
19+
email: this.user.email,
20+
password: this.user.password
21+
})
22+
.then(() => {
23+
// Logged in, redirect to home
24+
<% if (filters.ngroute) { %>this.$location.path('/');<% } %><% if (filters.uirouter) { %>this.$state.go('main');<% } %>
25+
})
26+
.catch(err => {
27+
this.errors.other = err.message;
28+
});
29+
}
30+
}
31+
}
2532

26-
});
33+
angular.module('<%= scriptAppName %>')
34+
.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: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
'use strict';
22

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

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-
});
14+
if (form.$valid) {
15+
this.Auth.changePassword(this.user.oldPassword, this.user.newPassword)
16+
.then(() => {
17+
this.message = 'Password successfully changed.';
18+
})
19+
.catch(() => {
20+
form.password.$setValidity('mongoose', false);
21+
this.errors.other = 'Incorrect password';
22+
this.message = '';
23+
});
24+
}
25+
}
26+
}
27+
28+
angular.module('<%= scriptAppName %>')
29+
.controller('SettingsController', SettingsController);

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

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

11-
<div class="form-group" ng-class="{ 'has-success': form.name.$valid && submitted,
12-
'has-error': form.name.$invalid && submitted }">
11+
<div class="form-group" ng-class="{ 'has-success': form.name.$valid && vm.submitted,
12+
'has-error': form.name.$invalid && vm.submitted }">
1313
<label>Name</label>
1414

15-
<input type="text" name="name" class="form-control" ng-model="user.name"
15+
<input type="text" name="name" class="form-control" ng-model="vm.user.name"
1616
required/>
17-
<p class="help-block" ng-show="form.name.$error.required && submitted">
17+
<p class="help-block" ng-show="form.name.$error.required && vm.submitted">
1818
A name is required
1919
</p>
2020
</div>
2121

22-
<div class="form-group" ng-class="{ 'has-success': form.email.$valid && submitted,
23-
'has-error': form.email.$invalid && submitted }">
22+
<div class="form-group" ng-class="{ 'has-success': form.email.$valid && vm.submitted,
23+
'has-error': form.email.$invalid && vm.submitted }">
2424
<label>Email</label>
2525

26-
<input type="email" name="email" class="form-control" ng-model="user.email"
26+
<input type="email" name="email" class="form-control" ng-model="vm.user.email"
2727
required
2828
mongoose-error/>
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
Doesn't look like a valid email.
3131
</p>
32-
<p class="help-block" ng-show="form.email.$error.required && submitted">
32+
<p class="help-block" ng-show="form.email.$error.required && vm.submitted">
3333
What's your email address?
3434
</p>
3535
<p class="help-block" ng-show="form.email.$error.mongoose">
36-
{{ errors.email }}
36+
{{ vm.errors.email }}
3737
</p>
3838
</div>
3939

40-
<div class="form-group" ng-class="{ 'has-success': form.password.$valid && submitted,
41-
'has-error': form.password.$invalid && submitted }">
40+
<div class="form-group" ng-class="{ 'has-success': form.password.$valid && vm.submitted,
41+
'has-error': form.password.$invalid && vm.submitted }">
4242
<label>Password</label>
4343

44-
<input type="password" name="password" class="form-control" ng-model="user.password"
44+
<input type="password" name="password" class="form-control" ng-model="vm.user.password"
4545
ng-minlength="3"
4646
required
4747
mongoose-error/>
4848
<p class="help-block"
49-
ng-show="(form.password.$error.minlength || form.password.$error.required) && submitted">
49+
ng-show="(form.password.$error.minlength || form.password.$error.required) && vm.submitted">
5050
Password must be at least 3 characters.
5151
</p>
5252
<p class="help-block" ng-show="form.password.$error.mongoose">
53-
{{ errors.password }}
53+
{{ vm.errors.password }}
5454
</p>
5555
</div>
5656

57-
<div class="form-group" ng-class="{ 'has-success': form.confirmPassword.$valid && submitted,
58-
'has-error': form.confirmPassword.$invalid && submitted }">
57+
<div class="form-group" ng-class="{ 'has-success': form.confirmPassword.$valid && vm.submitted,
58+
'has-error': form.confirmPassword.$invalid && vm.submitted }">
5959
<label>Confirm Password</label>
60-
<input type="password" name="confirmPassword" class="form-control" ng-model="user.confirmPassword"
61-
match="user.password"
60+
<input type="password" name="confirmPassword" class="form-control" ng-model="vm.user.confirmPassword"
61+
match="vm.user.password"
6262
ng-minlength="3" required/>
6363
<p class="help-block"
64-
ng-show="form.confirmPassword.$error.match && submitted">
64+
ng-show="form.confirmPassword.$error.match && vm.submitted">
6565
Passwords must match.
6666
</p>
6767
</div>

0 commit comments

Comments
 (0)