新 Web 项目上 javascript 配置的最佳实践

发布于 2024-09-08 07:40:47 字数 792 浏览 0 评论 0原文

我想问一个关于新的大型网络项目的 javascript 要求的问题。我们将使用大量的 javascript、ajax 请求、jquery、json 对象和我们项目中的 jquery 插件。我们计划在服务器端使用 php 类和 ini 文件将全局变量和许多默认值存储在全局站点配置文件中。

但是我们需要使用 javascript - jquery 读取、使用、有时覆盖客户端的一些变量和配置值。

这个基于 javascript 的配置文件必须具有以下属性;

  • 不会拥有所有服务器端配置值。只有我们需要。
  • 必须是一个将在 html head 部分调用的单个文件。
  • 必须定义一个全局变量或 json 或 javasctipt 对象或数组(我不知道哪个是最好的)
  • 这个值必须可以被其他 javascript 函数和对象访问。
  • 将存储布尔值、字符串、整数,也许是 5-6 个不同页面的一些初始化方法(例如:我们不需要产品详细信息页面上的主页配置值,并且我们不需要产品详细信息页面的一些初始化方法和值在主页等上)
  • 我们需要在每个页面上达到此配置对象的一些值,例如 debugMode=true 或 false 等。
  • 我们需要通过此配置文件了解其他 javascript 对象到运行平台的图像和其他资源路径(开发人员-测试-阶段-生产)

我们还可以在服务器端完全生成此文件或生成静态 .js 文件,并在 PHP 请求之后设置一些特定于用户页面或特定于语言的值,而不是我们必须放置的值(覆盖)服务器端在 Js 对象中生成的一些值。

该解决方案的最佳实践是什么?有什么建议吗?

I would like to ask a question about a new and large scale web project's javascript requirements. We will use lot of javascript, ajax requests, jquery, json objects & jquery plugins in our project. We planning to store global variables and lot of default values in a global site configuration file with php class and ini file on server-side.

But we need to read, use and sometimes override some variables and configuration values on client-side with javascript - jquery.

This javascript based configuration file must have following properties;

  • Won't have all server-side config values. Only we need.
  • Must be a single file that will be call on html head section.
  • Must define a global variable or json or javasctipt object or array (I don't know which is best)
  • This values must reachable by other javascript functions and objects.
  • Will store booleans, strings, integers maybe a some little initialization methods for 5-6 different pages (ex.: we don't need main page's config values on product detail page's and we don't need product detail page's some initialization methods and values on main page etc.)
  • We need to reach some values of this configuration object on every page like debugMode=true or false etc..
  • We need to know in other javascript objects to running platform via this config file for images and other resource paths (Developer-Test-Stage-Production)

Also we can completely generate this file on server side or generate a static .js file and after a PHP request, set some user-page-specific or language specific values, than we must be put (override) some of this server-side generated values in Js object.

What is best-practices for this solution? Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-09-15 07:40:47
  • 必须定义全局变量或 json
    或 javasctipt 对象或数组(我不
    知道哪个最好)

JSON 基本上是一个对象文字,所以它可以做到这两点。大胆试试吧。将 JSON 视为序列化的 javascript 对象。

  • 该值必须
    可通过其他 JavaScript 访问
    函数和对象。

一旦您运行 JSON,它就会在您的代码中可用。

  • 将存储
    布尔值、字符串、整数可能是
    一些小的初始化方法
    5-6 个不同的页面(例如:我们不
    需要主页的配置值
    产品详细信息页面,但我们没有
    需要产品详细页面的一些
    初始化方法和值
    主页等)

同样,JSON 可以完成所有这些工作。

因此,我建议使用一个 JSON 文件,该文件通过客户端的脚本标记包含在内。 JSON 很容易在服务器端生成、读取和操作(例如:php 中的 json_encodejson_decode)。

它应该是一个静态 js 文件,因为它对服务器的压力最小。此外,Gzip 压缩有助于降低带宽成本。

  • Must define a global variable or json
    or javasctipt object or array (I don't
    know which is best)

JSON is basically an object literal, so it can do both. Go for it. Think of JSON as a serialized javascript object.

  • This values must
    reachable by other javascript
    functions and objects.

As soon as you run the JSON it will be available in your code.

  • Will store
    booleans, strings, integers maybe a
    some little initialization methods for
    5-6 different pages (ex.: we don't
    need main page's config values on
    product detail page's and we don't
    need product detail page's some
    initialization methods and values on
    main page etc.)

Again, JSON can do all of that.

So I would suggest a JSON file, that is included via script tag on the client side. JSON is easy to generate, read and manipulate on the server side (eg.: json_encode, json_decode in php).

It SHOULD BE a static js file, as it stresses the server the least. Also, Gzip compression can help to keep the bandwidth cost low.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文