角度函数依赖

时间:2015-02-19 03:24:00

标签: javascript angularjs

我在哪里放置常见的handleErrorhandleSuccess函数。

这些是每项服务将使用的常用功能。放置这些功能的最佳位置在哪里。我应该将这些作为全局函数并将它们作为依赖项注入。

(function () {
    "use strict";
    angular.module('myApp.Group')
        .service('GroupService', function ($http, $q, $location, Environment) {



            // I transform the error response, unwrapping the application dta from
            // the API response payload.
            function handleError(response) {

                // The API response from the server should be returned in a
                // nomralized format. However, if the request was not handled by the
                // server (or what not handles properly - ex. server error), then we
                // may have to normalize it on our end, as best we can.
                if (!angular.isObject(response.data) || !response.data.message) {
                    return ($q.reject("An unknown error occurred."));
                }

                // Otherwise, use expected error message.
                return ($q.reject(response.data.message));

            }


            // I transform the successful response, unwrapping the application data
            // from the API response payload.
            function handleSuccess(response) {
                return (response.data);
            }

            this.remove = function (id) {
                var request = $http({
                        method: "delete",
                        url: '/group/' + id
                    });
                return (request.then(handleSuccess, handleError));
            };
        });
}());

1 个答案:

答案 0 :(得分:2)

以下是我们的工作方式:

我们使用$ httpProvider拦截响应并自行处理每个代码。我们提供了一项服务来处理这项功能。

我们的app配置如下所示:

appModule.config(['$routeProvider', '$locationProvider', '$httpProvider', '$provide',
function ($routeProvider, $locationProvider, $httpProvider, $provide) {
    // Http interceptor to handle session timeouts and basic errors
    $httpProvider.responseInterceptors.push(['httpHandlersSrv', function (httpHandlersSrv) {
        return function (promise) { return promise.then(httpHandlersSrv.success, httpHandlersSrv.error); };
    }]);
    routeProvider = $routeProvider;
    $locationProvider.html5Mode(true);
}
]);

这就是我们的$ httpHandlersSrv看起来像处理错误的地方。请注意,我们只是传递成功的响应而不做任何事情:

angular.module('appModule').factory('httpHandlersSrv', ['$q', '$location', '$rootScope', 'toaster', '$window', function ($q, $location, $rootScope, toaster, $window) {
return {
    success: function (response) {
        return response;
    },
    error: function (response) {
        switch (response.status) {
            case 0:
                //Do something when we don't get a response back
                break;
            case 401:
                //Do something when we get an authorization error
                break;
            case 400:
               //Do something for other errors
                break;
            case 500:
               //Do something when we get a server error
                break;
            default:
                //Do something with other error codes
                break;
        }
        return $q.reject(response);
    }
};
}]);