javascript - Best Approach for Configuration File for an AngularRequireJS Application? - Stack Overflow

Is there a monly accepted best practice for maintaining a configuration file that is available on the c

Is there a monly accepted best practice for maintaining a configuration file that is available on the client side (sort of an equivalent of the AppSettings section on the server side in an ASP.NET application)?

Our application is based on Angular. Our desire is to externalize environment-specific settings (like remote system URLs, etc) from the code itself, so that ideally an Ops person and not a developer can modify settings in one place.

Thanks in advance for any insight!

Is there a monly accepted best practice for maintaining a configuration file that is available on the client side (sort of an equivalent of the AppSettings section on the server side in an ASP.NET application)?

Our application is based on Angular. Our desire is to externalize environment-specific settings (like remote system URLs, etc) from the code itself, so that ideally an Ops person and not a developer can modify settings in one place.

Thanks in advance for any insight!

Share Improve this question asked May 2, 2013 at 18:25 blasterblaster 8,93711 gold badges51 silver badges78 bronze badges 2
  • 1 you can keep settings in a dedicated config.js file, editing JSON is not more difficult then editing XML – Guillaume86 Commented May 2, 2013 at 20:13
  • 1 Regarding Angular, maybe this answer could inspire you further stackoverflow./questions/16339595/… – kfis Commented May 3, 2013 at 7:38
Add a ment  | 

3 Answers 3

Reset to default 4

I don't think it's a good idea to use a config.js file when developing AngularJS apps. The reason being, you will break any possibility for automatic testing.

Instead, I create a "Settings" service in which I specify my app specific context. For example:

angular.module('settings',[]).factory('SettingsService',function(){

   var service={};

   // Insert your settings here
   service.ServerPath = 'whateverwhatever';
   service.ImagePath  = 'bla bla bal';

   return service;

});

Then inject the SettingsService into controllers that need access to the settings.

Ofcourse, (which I omitted for simplicity here), you could instead create the ServiceService with an empty body, and then in the application .run() method fetch the settings from the server.

I use the simple described way, and maintain a SettingsService for each deployment type ("development", "test", "production" etc). Then just include the right SettingsService in your build-script depending on the target (I use Grunt, but you could use Ant also).

I think the answer to this is very dependent on how you setup your angular layer in the overall structure of the application.

For example, I have served up an angular application the same way for each environment from the base server application (Grails, node.js, RoR) and the angular application is always the same code base because the data I am feeding to angular is controlled by the server side and the angular calls are all relative to the base URL. In other words, I proxy all data through the server app that serves up the Angular application so all the angular app "knows about" is its own url.

If you are directly calling other services and not using the base server application to proxy the data, then maybe you can still use the server to feed you the url's depending on some config on the server side of the application. In this case, you can always store those config values in a DB table so changing them on the fly is possible.

I have this same dilemma, and what I have resorted to for right now is: I have a set of config files, like config-local.js, config-staging.js, etc. Then I create a symlink to config.js anywhere I want to run my app. On my local desktop, I symlink to config-local.js, in the staging env, I symlink to config-staging.js. Then I put config.js in .gitignore.

Pros: It works. Cons: Requires symlink creation before the app will work correctly.

Future: Maybe add a task in grunt to create the symlink?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744765679a4592444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信