1

所以我有一个主模块app定义为

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/...

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

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()}
    }
}])

即我想url在子模块中使用 absolute app.social_accounts

但是当我将Album(在 下注册app.social_accounts)注入到controller DashboardCtrlapp时,请求被发送到http://localhost:8000/app/me/albums/.

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

4

1 回答 1

2

任何定义的配置restmodProvider都是全局的restmod,与它所使用的模块无关。因此在上面的示例中,模块中urlPrefix定义的配置被模块app.social_accounts中的配置覆盖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 来保持 DRY:

.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');
});
于 2015-01-04T03:55:21.477 回答