2015-01-02 71 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'); 
});