main.js 3.37 KB
// 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);
    });
}]);