如何为$ resource指定主机名?

时间:2014-08-20 13:54:15

标签: angularjs angularjs-resource

我以这样的方式定义资源,根据配置文件,主机名可以更改。当我在本地开发它是localhost时,当我们将它部署到qa时它是其他主机,在生产中它又是一个不同的主机。我们通过配置文件来控制它。现在,当我定义资源时,我们会做这样的事情

 angular.module('app')
     .factory('InsuredDemographics', ['$resource', 'config', function ($resource, config) {

         var memberServicesHostName = config.memberServicesHostName;
         return $resource(memberServicesHostName + '/insureds/:insuredId/raceethnicitylanguage', null,
         {
             'update': { method: 'POST', 'withCredentials': true },
             'save': { method: 'POST', 'withCredentials': true }
         });
     }]);

如果没有覆盖网址但是看看这个

,这很好
 angular.module('app')
    .factory('Dependents', ['$resource', 'config', function ($resource, config) {

        var memberServicesHostName = config.memberServicesHostName;
        return $resource(memberServicesHostName + '/Insureds/:primaryInsuredId/Dependents', null,
        {
            'query': {
                method: 'GET', withCredentials: true,
                url: memberServicesHostName + '/Insureds/:primaryInsuredId/Dependents/',
                isArray: false,
                interceptor:
                       {
                           response: function (response) {

                               response.resource.dependents.forEach(function (dependent) {

                                   if (dependent.type.code == 'CHLD') dependent.type.description = "Child";
                               });

                               return response.resource;
                           }
                       }
            },
            'get': {
                method: 'GET', withCredentials: true,
                url: memberServicesHostName + '/Insureds/:primaryInsuredId/Dependents/:insuredIdOfDependent',
                interceptor:
                   {
                       response: function (response) {

                           if (response.resource.dependent.type.code == 'CHLD') response.resource.dependent.type.description = "Child";

                           return response.resource;
                       }
                   }
            },
            'save': {
                method: 'POST', withCredentials: true,
            },
            'update': {
                method: 'PUT', withCredentials: true,
                url: memberServicesHostName + '/Insureds/:primaryInsuredId/Dependents/:insuredIdOfDependent'
            }
        });
    }]);

我必须在每个自定义网址前加上主机名。这不能在资源级别指定吗?

0 个答案:

没有答案