当我像这样启动流星项目时,我正在使用环境变量
MYVAR1="foo bar sdf" MYVAR2=0 meteor
然后在文件 [root]/lib/constants.js 中我有
ENV = ["sjobs", "unisight", "dfgdfsgf"];
AUTHENTICATION = true;
if (Meteor.isClient) {
Meteor.call("getGlobals", function (error, result) {
"use strict";
if (error === undefined) {
AUTHENTICATION = result.AUTHENTICATION;
ENV= result.ENV;
console.log(result);
} else {
console.error(error);
}
});
}
if (Meteor.isServer) {
var univasENV = ["urb", "unisight", "sjobs", "unicloud"];
var tmpenv;
if (process.env.MYVAR2 !== undefined && parseInt(process.env.MYVAR2, 10) === 1) {
AUTHENTICATION = false;
}
if (process.env.MYVAR1 !== undefined) {
tmpenv = process.env.MYVAR1.split(" ");
ENV = [];
_.each(tmpenv, function (value) {
"use strict";
if (univasENV.indexOf(value) !== -1) {
ENV.push(value);
}
});
}
}
在另一个文件 [root]/server/methods.js 中我有:
Meteor.methods({
getGlobals: function(){
"use strict";
console.log(AUTHENTICATION, ENV);
return {
auth: AUTHENTICATION,
env: ENV
};
}
});
服务器部分按我的预期工作,但是客户端中的代码是在所有内容都渲染或加载后执行的。这里的问题是 Meteor.call() 运行异步,我无法使用(据我所知)Meteor.wrapAsync()。
我还尝试在集合中写入我需要的那些值,然后从客户端读取它(所有这些都在 [root]/lib/constants.js 中),但它的行为与方法/调用完全相同。
所以问题是如何在一开始就将一些值从服务器传递到客户端?