如何在Yeoman Ember应用程序中设置/获取特定于环境的variables

考虑到Yeoman Ember应用程序。

我看过不同的工具,如:

  • https://github.com/logankoester/grunt-environment
  • https://github.com/jsoverson/grunt-env
  • https://github.com/outaTiME/grunt-config

但是我不太清楚如何在router.js中设置/获取不同的适配器url,具体取决于一些grunt / node环境。

一种方法是使用两个文件:development.properties和production.properties,使用grunt可以将它们复制到current.properties文件中,然后可以随时读取current.properties文件,例如使用来自任何地方的nconf你的应用程序。

你只需要在你的grunt文件中使用grunt-contrib-copy任务。

简介:创build一个从命令行读取环境的grunt文件,并将相应的文件属性复制到current.properties,然后在您的route.js中只包含nconf,然后使用它。

另外,作为替代方法,nconf可以在运行时从命令行读取参数,因此您可以避免使用grunt文件,并像节点app.js –property = value那样运行它,并使用nconf的属性值你的route.js

所以我最终找出了一个行之有效的方法,但只有两个env:

我为烬发生器提出了一个请求,开始使用这种模式: https : //github.com/borisrorsvort/generator-ember/commit/66f26e9e5a7599da53cb99b85e8ef5864648349b

这里是一个与yeoman烬宝生成器一起工作的实现:

你的replace任务应该是这样的:

replace: { app: { options: { variables: { ember: 'bower_components/ember/ember.js', ember_data: 'bower_components/ember-data-shim/ember-data.prod.js', app_config: 'scripts/app_config/development.js' } }, files: [{ src: '<%= yeoman.app %>/index.html', dest: '.tmp/index.html' }] }, dist: { options: { variables: { ember: 'bower_components/ember/ember.prod.js', ember_data: 'bower_components/ember-data-shim/ember-data.prod.js', app_config: 'scripts/app_config/production.js' } }, files: [{ src: '<%= yeoman.app %>/index.html', dest: '.tmp/index.html' }] } } 

======

然后在@@ ember和@@ ember_data下添加'@@ app_config'的index.html,使其被replace为正确的string

======

然后创build两个文件:

  • 应用程序/模板/脚本/ APP_CONFIG / development.js
  • 应用程序/模板/脚本/ APP_CONFIG / production.js

这些包含一个AppConfig对象,您可以在您的Ember应用程序中使用

 var AppConfig = {}; 

======

现在想象一下,当您构build应用程序时,您想要更改适配器:

只要使用:

 MyApp.ApplicationAdapter = DS.RESTAdapter.extend({ host: AppConfig.apiAdapterUrl });