Ember.js Ember中的单独环境配置
我正在使用Ember.js Ember中的单独环境配置,ember.js,configuration,ember-data,ember-cli,ember-cli-addons,Ember.js,Configuration,Ember Data,Ember Cli,Ember Cli Addons,我正在使用ember cli来构建我的应用程序,它为我提供了一个很好的app.js文件,我可以在静态资产服务器上安装该文件。允许在部署时进行单独配置的最惯用方法是什么 例如,我可能会告诉我的app.js文件的使用者包含一个额外的config.[js | json]文件,该文件中的值将加载到ENV对象中。。。因此,我可以将应用程序指向不同的REST端点,例如(QA、沙盒、预发布等),而无需重新编译 我想一定有办法,我只是看不见而已。我知道有config/environment.js文件,但它被编译
ember cli
来构建我的应用程序,它为我提供了一个很好的app.js
文件,我可以在静态资产服务器上安装该文件。允许在部署时进行单独配置的最惯用方法是什么
例如,我可能会告诉我的app.js
文件的使用者包含一个额外的config.[js | json]
文件,该文件中的值将加载到ENV
对象中。。。因此,我可以将应用程序指向不同的REST端点,例如(QA、沙盒、预发布等),而无需重新编译
我想一定有办法,我只是看不见而已。我知道有config/environment.js
文件,但它被编译到dist
文件夹中。我正在寻找一些东西,坐在旁边的打包JS。我当然可以一起破解一些东西,所以我不是在寻找破解。可能是一个ember cli插件
?我想一定有一种“余烬方式”可以做到这一点
我就是找不到:)好吧,我就是这么做的。基本上,我允许主机应用程序覆盖某些设置。我注册了一个初始值设定项,将它们插入到配置对象中,然后像普通一样使用配置选项。它看起来有点像这样: config/environment.js
// This is just normal ENV.APP configuration stuff. Nothing odd here
module.exports = function(environment) {
var ENV = {
// snip
APP: {
API_HOST: 'http://defaultAPIHost.com',
AUTH_PROVIDER: 'http://defaultAuthProvider.com'
}
};
return ENV;
};
import config from '../config/environment';
// This is the custom stuff. If the values have been defined globally,
// override them on the config object. I suppose this can be done a
// bit more dynamically, but this explicit code is for illustrative purposes.
export function initialize() {
let apiOverride = window.MyAppEnv && window.MyAppEnv.API_HOST;
let authOverride = window.MyAppEnv && window.MyAppEnv.AUTH_PROVIDER;
config.APP.API_HOST = apiOverride || config.APP.API_HOST;
config.APP.AUTH_PROVIDER = authOverride || config.APP.AUTH_PROVIDER;
}
export default {
name: 'parameter-overrides',
initialize: initialize
};
app/initializers/parameter overrides.js
// This is just normal ENV.APP configuration stuff. Nothing odd here
module.exports = function(environment) {
var ENV = {
// snip
APP: {
API_HOST: 'http://defaultAPIHost.com',
AUTH_PROVIDER: 'http://defaultAuthProvider.com'
}
};
return ENV;
};
import config from '../config/environment';
// This is the custom stuff. If the values have been defined globally,
// override them on the config object. I suppose this can be done a
// bit more dynamically, but this explicit code is for illustrative purposes.
export function initialize() {
let apiOverride = window.MyAppEnv && window.MyAppEnv.API_HOST;
let authOverride = window.MyAppEnv && window.MyAppEnv.AUTH_PROVIDER;
config.APP.API_HOST = apiOverride || config.APP.API_HOST;
config.APP.AUTH_PROVIDER = authOverride || config.APP.AUTH_PROVIDER;
}
export default {
name: 'parameter-overrides',
initialize: initialize
};
应用程序/适配器/应用程序
import DS from 'ember-data';
import config from '../config/environment';
// Then consume the config properties as you normally would
export default DS.RESTAdapter.extend({
host: config.APP.API_HOST,
namespace: "api"
});
现在,宿主应用程序可以在页面中包含这些内容,它将覆盖config/environment.js
中的值:
<script type="text/javascript">
// Override this value in production to the proper API host and Auth host
window.MyAppEnv = {
AUTH_PROVIDER: null, //'http://oauthhost.com/OAuth2'
API_HOST: null //"http://apihost.com"
};
</script>
//将生产中的此值重写为正确的API主机和身份验证主机
window.MyAppEnv={
身份验证提供程序:null,/'http://oauthhost.com/OAuth2'
API_主机:空/“http://apihost.com"
};
这是否合理的做法?外面有更好的吗 为什么您希望它与打包的JS一起成为一个文件?这样数据模型就可以指向不同的后端,而无需重新构建.ember-cli,因为它实际上与ember无关,但更重要的是它的构建方式:)这有帮助吗?您可以使用一个全局文件在您的环境配置中启动属性,然后从index.html页面插入该全局文件(或在那里覆盖它),以避免它成为构建的一部分。就我个人而言,我喜欢做不同的构建定义,并将其全部放在environment.js文件中,但老实说,这只是一种偏好。