2015-01-02 27 views
1

所以我有一个主模块app定义为配置在AngularJS restmod的子模块

app = angular.module("app", ['app.social_accounts', 'restmod']) 

有其restmod模块,配置:

app.config(function(restmodProvider) { 
    restmodProvider.rebase({ 
    $config: { 
     primaryKey: "id", 
     style: "ams", 
     urlPrefix: "/app/" 
    } 
    }); 
}); 

,它按预期工作:请求被送到http://localhost:8000/app/...

现在我想在子模块app.social_accounts中使用restmod,通过做

app = angular.module("app.social_accounts", ['restmod']) 

app.config(function(restmodProvider) { 
    restmodProvider.rebase({ 
    $config: { 
     primaryKey: "id", 
     style: "ams", 
     urlPrefix: "https://graph.facebook.com/" 
    } 
    }); 
}); 
app.factory("Album", ["restmod", function(restmod){ 
    Album = restmod.model("/me/albums/") 
    return { 
     "get": function(){Album.$search()} 
    } 
}]) 

即我想在子模块app.social_accounts使用绝对url

但是,当我注入Albumapp.social_accounts下注册)到一个controllerDashboardCtrlapp下,该请求被送到http://localhost:8000/app/me/albums/

所以我想知道这里发生了什么,以及如何在app.social_accounts下如何实现restmod的单独url

回答

2

restmodProvider定义的任何配置是全局的restmod不管它使用的模块。因此,在你上面的例子中,app.social_accounts模块中定义的urlPrefix正在由app模块中的配置覆盖。

为了达到你所期望的行为,你可以在每个模型的基础覆盖配置:

angular.module('app.social_accounts', ['restmod']) 

    .factory('Album', function(restmod) { 
    var Album = restmod.model('/me/albums') 
     .mix({ 
     $config: { 
      urlPrefix: 'https://graph.facebook.com/' 
     } 
     }); 
    }); 

如果你需要在一个模块内的多个模型的配置,一个mixin可以使用保持干燥:

.factory('restmodConfigSocial', function(restmod) { 
    return restmod.mixin({ 
    $config: { 
     urlPrefix: 'https://graph.facebook.com/' 
    } 
    }); 
}) 

.factory('Album', function(restmod) { 
    var Album = restmod.model('/me/albums').mix('restmodConfigSocial'); 
});