navbar.directive.js
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(function() {
'use strict';
angular
.module('angular')
.directive('acmeNavbar', acmeNavbar);
/** @ngInject */
function acmeNavbar() {
var directive = {
restrict: 'E',
templateUrl: 'app/components/navbar/navbar.html',
scope: {
creationDate: '='
},
controller: NavbarController,
controllerAs: 'vm',
bindToController: true
};
return directive;
/** @ngInject */
function NavbarController(moment, $modal, AuthService, Session, $scope, AUTH_EVENTS) {
var vm = this;
// "vm.creation" is avaible by directive option "bindToController: true"
vm.relativeDate = moment(vm.creationDate).fromNow();
vm.currentUser = Session.getCurrentUser();
vm.modalInstance = null;
vm.openLogin = function() {
vm.modalInstance = $modal.open({
templateUrl: 'app/components/auth/login.html',
controller: 'AuthController',
controllerAs: 'vm',
bindToController: true
});
};
vm.logout = function() {
AuthService.logout();
};
$scope.$on(AUTH_EVENTS.loginSuccess, function() {
if(vm.modalInstance) {
vm.modalInstance.close();
vm.modalInstance = null;
}
vm.currentUser = Session.getCurrentUser();
});
$scope.$on(AUTH_EVENTS.logoutSuccess, function() {
vm.currentUser = Session.getCurrentUser();
});
}
}
})();