有没有人使用 javascript 的配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2775838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does anyone use config files for javascript?
提问by Justin Helgerson
We have javascript files that are environment specific, and so I was thinking of going down the path of creating a generic way to read in an XML (config) file to store different environment specific settings. I was curious to know if anybody else on here does that (or if not, is there a reason why you don't)?
我们有特定于环境的 javascript 文件,因此我正在考虑创建一种通用方式来读取 XML(配置)文件以存储不同的环境特定设置。我很想知道这里是否还有其他人这样做(或者如果没有,您是否有理由不这样做)?
采纳答案by Diodeus - James MacFarlane
JSON is hundreds of times faster to consume than XML, bring a native JavaScript object itself. Attach and forget.
JSON 的使用速度比 XML 快数百倍,自带原生 JavaScript 对象本身。附加和忘记。
EDIT:
编辑:
James Westgate's example is JSON. You can use this inline or as an external file or even loaded via AJAX.
James Westgate 的示例是 JSON。您可以使用此内联文件或作为外部文件使用,甚至可以通过 AJAX 加载。
Here is another example:
这是另一个例子:
var clientData = {}
clientData.dataset1 = [
{name:'Dave', age:'41', userid:2345},
{name:'Vera', age:'32', userid:9856}
]
alert(clientData.dataset1[0].name)
回答by James Westgate
All you need to do is load a javascript file with some variable definitions, ideally namespaced. You can use a single object literal for this:
您需要做的就是加载一个带有一些变量定义的 javascript 文件,最好是命名空间。您可以为此使用单个对象文字:
var config = {
option1: 'good;',
option2: {name: 'bad'},
option3: ['u','g','l','y']
}
Then load that file as your first script and you will have access to config information in each subsequent script eg
然后将该文件加载为您的第一个脚本,您将可以访问每个后续脚本中的配置信息,例如
if (config.option1 == 'good') doStuff();
回答by Steve Wortham
Why not use a separate js file to store your environment-specific settings?
为什么不使用单独的 js 文件来存储特定于环境的设置?
Just like you can use multiple CSS files to style your page, you can use multiple js files as well.
就像您可以使用多个 CSS 文件来设置页面样式一样,您也可以使用多个 js 文件。
So you could create a file called app-config.js with specific settings:
因此,您可以使用特定设置创建一个名为 app-config.js 的文件:
var SiteName = "MyWebsite.com";
var HeaderImage = "http://mycdn.com/images/mywebsite/header.png";
And then you include the js on your pages like this:
然后你像这样在你的页面上包含 js:
<script type="text/javascript" src="/js/app-config.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
回答by Pointy
One thing you might consider is to have the pages themselves include little Javascript blocks to provide just this sort of configuration information. Often, you really only need a small amount of information for URL building etc. I'll give a JSP example:
您可能会考虑的一件事是让页面本身包含很少的 Javascript 块来提供此类配置信息。通常,您实际上只需要少量信息来构建 URL 等。我将举一个 JSP 示例:
<script>
var appConfig = {
'syndicate': '${environ.syndicate}',
'urlBase': '${environ.urlBase}'
};
</script>
Then your "pure" Javascript code can look to window.appConfigto get critical information. Obviously this would get to be a mess if you needed a whole lot of stuff.
然后您的“纯”Javascript 代码可以查看window.appConfig以获取关键信息。显然,如果你需要很多东西,这会变得一团糟。
This is particularly easy when your pages are built via some templating system in your server-side environment. That way, you only have to set the script tag up in one place (or a small number of places; the templates in other words) and all the pages get it automatically.
当您的页面是通过服务器端环境中的某个模板系统构建时,这尤其容易。这样,您只需在一个地方(或少数地方;换句话说,模板)设置脚本标记,所有页面都会自动获取它。
(edited to eliminate weird variable declaration)
(编辑以消除奇怪的变量声明)
回答by Tufan
I'd recommend using YCB. Here is an example how to use it: https://github.com/yahoo/ycb/blob/master/examples/simple/app.js
我建议使用YCB。这是一个如何使用它的示例:https: //github.com/yahoo/ycb/blob/master/examples/simple/app.js
回答by Joel Martinez
You can easily pull down XML files using something like jquery (http://think2loud.com/reading-xml-with-jquery/).
您可以使用类似 jquery ( http://think2loud.com/reading-xml-with-jquery/) 之类的东西轻松下拉 XML 文件。
However, I'd like to pose whether environment specific client-side javascript code is really a good idea. Seems like any environment specific (ie. qa, uat, production I assume) should be handled on the server and the client should be environment agnostic
但是,我想提出特定于环境的客户端 javascript 代码是否真的是一个好主意。似乎任何特定于环境的(即我假设的 qa、uat、生产)都应该在服务器上处理,而客户端应该与环境无关

