forked from AngularYii2/angularyii2.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (89 loc) · 3.06 KB
/
app.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'toaster', 'ngSanitize', 'mgcrea.ngStrap']);
app.config(['$locationProvider', '$routeProvider', '$httpProvider', function ($locationProvider, $routeProvider, $httpProvider) {
var modulesPath = 'modules';
$routeProvider
.when('/', {
templateUrl: modulesPath + '/site/views/main.html'
})
.when('/login', {
templateUrl: modulesPath + '/site/views/login.html',
controller: 'SiteLogin'
})
.when('/post', {
templateUrl: modulesPath + '/post/views/index.html',
controller: 'PostIndex'
})
.when('/post/create', {
templateUrl: modulesPath + '/post/views/form.html',
controller: 'PostCreate'
})
.when('/post/:id/edit', {
templateUrl: modulesPath + '/post/views/form.html',
controller: 'PostEdit'
})
.when('/post/:id/delete', {
templateUrl: modulesPath + '/post/views/delete.html',
controller: 'PostDelete'
})
.when('/post/:id', {
templateUrl: modulesPath + '/post/views/view.html',
controller: 'PostView'
})
.when('/404', {
templateUrl: '404.html'
})
.otherwise({ redirectTo: '/404' })
;
$locationProvider.html5Mode(true).hashPrefix('!');
$httpProvider.interceptors.push('authInterceptor');
}]);
app.factory('authInterceptor', function ($q, $window) {
return {
request: function (config) {
if ($window.sessionStorage._auth && config.url.substring(0,4) == 'http' ) {
config.params = {'access-token': $window.sessionStorage._auth};
}
return config;
},
responseError: function (rejection) {
if (rejection.status === 401) {
$window.location = 'login';
}
return $q.reject(rejection);
}
};
});
app.value('app-version', '0.1');
// Need set url REST Api in controller!
app.service('rest', function ($http, $location, $routeParams) {
return {
url: undefined,
models: function () {
return $http.get(this.url + location.search);
},
model: function () {
return $http.get(this.url + "/" + $routeParams.id);
},
get: function () {
return $http.get(this.url);
},
postModel: function (model) {
return $http.post(this.url, model);
},
putModel: function (model) {
return $http.put(this.url + "/" + $routeParams.id, model);
},
deleteModel: function (model) {
return $http.delete(this.url + "/" + $routeParams.id, model);
}
};
});
app.directive('login', ['$http', function ($http) {
return {
transclude: true,
link: function (scope, element, attrs) {
scope.isGuest = window.sessionStorage._auth == undefined;
},
template: '<a href="login" ng-if="isGuest">Login</a>'
}
}]);