main.js
3.37 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
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
117
// angular 主程序js,必须先导入,配置一些全局设置
var ScheduleApp = angular.module('ScheduleApp', [
'ui.router', // ui-route跳转
'ui.bootstrap', // ui bootstrap封装
'oc.lazyLoad', // 动态加载模块(html,js,css等)
'ngSanitize', // 净化html标签,配合ng-bind-html使用
'ngResource' // resource服务
]);
/**
* 用于请求通知。
*/
ScheduleApp.factory('requestNotificationChannel', ['$rootScope', function($rootScope) {
// 通知消息常量
var _START_REQUEST_ = '_START_REQUEST_'; // 开始请求通知message
var _END_REQUEST_ = '_END_REQUEST_'; // 请求结束通知message
// 计数器
var activeCalls = 0;
// 发布开始请求通知
var requestStarted = function() {
activeCalls += 1;
console.log("activeCalls=" + activeCalls);
$rootScope.$broadcast(_START_REQUEST_);
};
// 发布请求结束通知
var requestEnded = function() {
activeCalls -= 1;
console.log("activeCalls=" + activeCalls);
$rootScope.$broadcast(_END_REQUEST_);
};
/**
* 订阅开始请求通知。
* @param $scope 作用域
* @param handler 通知处理器函数
*/
var onRequestStarted = function($scope, handler) {
$scope.$on(_START_REQUEST_, function(event) {
handler();
});
};
/**
* 订阅请求结束通知。
* @param $scope 作用域
* @param handler 通知处理器函数
*/
var onRequestEnded = function($scope, handler) {
$scope.$on(_END_REQUEST_, function(event) {
handler();
});
};
return {
requestStarted : requestStarted,
requestEnded : requestEnded,
onRequestStarted : onRequestStarted,
onRequestEnded : onRequestEnded
};
}]);
// http 拦截器
ScheduleApp.factory('myInterceptor', ['requestNotificationChannel', function(requestNotificationChannel) {
return {
request: function(config) {
requestNotificationChannel.requestStarted();
return config;
},
requestError: function(rejection) {
requestNotificationChannel.requestEnded();
return rejection;
},
response: function(response) {
requestNotificationChannel.requestEnded();
return response;
},
responseError: function(rejection) {
requestNotificationChannel.requestEnded();
return rejection;
}
};
}]);
ScheduleApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
$httpProvider.interceptors.push('myInterceptor');
}]);
/** ocLazyLoader 配置 */
ScheduleApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
// TODO:全局配置在这里
});
}]);
/** 配置全局配置信息 */
ScheduleApp.factory('settings', ['$rootScope', function($rootScope) {
// 封装 settings服务,并设置给 $rootScope同名对象
var settings = {
// TODO:
};
$rootScope.settings = settings;
return settings;
}]);
/** Schedule App 主应用控制器 */
ScheduleApp.controller('ScheduleAppController', ['$scope', function($scope) {
$scope.$on('$viewContentLoaded', function(event) {
console.log("子页面已载入:" + event);
});
}]);