Configuration object in javascript

Storing configuration data (json)

tl;dr Why storing configuration data in json files is considered a de facto standard? I’ve recently read some parts of the Maintainable Javascript book, specially the Storing configuration data chapter. This is a quote from the chapter:

Configuration data is best stored in a separate file to create a clean separation between it and application logic. A good starting point is to have a separate JavaScript file for configuration data. Once the configuration data is in a separate file, it opens up more possibilities for managing that data. A worthwhile option is moving your configuration data into a non-JavaScript file. Even though you’re writing a JavaScript application, JavaScript isn’t a great way to store configuration data. That’s because the syntax is still that of a programming language, so you need to be sure you haven’t introduced syntax errors.

He basically says that storing configuration data in .json files or .js files is a bad practice and should be avoided. From json.org:

Interchange means to me to send and receive data, to communicate two processes in a well-formatted way. A lot of people are storing the data in json files, which adds a lot of noise: curly braces, indentation, double quotes, commas, cannot write comments, etc. Java uses key-value lines, is there a simpler way to store this data?

Microsoft likes to use .ini files which add sections to these key-value lines. Unix configuration files also use key-value lines. Then, if all the world uses key-value lines because they are easy to read/write by humans, why are json files a de facto standard in the node.js world? Please don’t tell me that’s because json and javascript are practically synonyms and because developers are too lazy and prefer to call require(«./config.json») . In the above examples, the first option (1) was born to ease the data interchangeability. And the second, imo, it’s just bad. This is the same as storing configuration data in a Java class. If I want to read this file from php because I have an apache server and nodejs for real-time things, how I’m supposed to parse that file? I’d need a javascript parser. Both options shouldn’t have syntax errors or the program might crash.

Источник

Best-practice for javascript configuration on new web project

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.) 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.

Читайте также:  Html text fixed height

Best-practice for javascript configuration on new web project

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 object s 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?

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

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.

Читайте также:  Java runtime set path

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.

Config, Running in this configuration, the port and dbName elements of dbConfig will come from the default.json file, and the host element will come from the production.json override file. Articles. Configuration Files. Special features for JavaScript configuration files; Common Usage; Environment Variables; Reserved Words; …

Creating a config for a function in JavaScript

I want my JavaScript class to be configurable from the outside. So, I created a private config -property and a loadConfig -method. My problem is, that when I load a config, it overwrites the config- property completely and not just the properties I defined.

(function() < var config = < param: value, group: < groupParam: value >>; function MyClass() < this.loadConfig = function(loadConfig) < if (typepof loadConfig == "undefined" || typeof loadConfig == "null") return; else config = loadConfig; >; > window.MyClass = new MyClass(); >)(); 

When I load a custom config

  

I want config.param to still be «value», while config.group.groupParam is «newValue».

Currently, the object is overwritten and after loadConfig() , config.param doesn’t exist anymore.

If you don’t want to reinvent the wheel, jQuery has a great function called $.extend() which seems to be what you need.

You can check it out in the jQuery 1.6.1 source for example ( Ctrl + F and «extend» => been a long time since I’ve been waiting to use these keyboard things once 🙂 ).

It’s overwritten because you’re telling it to overwrite with this line in your loadConfig method:

In order to preserve any values that loadConfig does not explicitly change, you’ll have to loop through all of the properties in loadConfig and assign them individually:

this.loadConfig = function (loadConfig) < if (typeof loadConfig == "undefined" || typeof loadConfig == "null") < return; >else < var x; for (x in loadConfig) < if (Object.prototype.hasOwnProperty.call(loadConfig, x)) < config[x] = loadConfig[x]; >> > >; 

Configure embedded Power BI report settings in a, Besides giving the API access to the report, you can also use the configuration object to customize your report’s appearance and behavior. For instance, you can adjust the filter visibility, navigation access, and location settings in the configuration object. The following sections explain how to embed and …

How to make and reference a configuration file in javascript

I have a file that I am trying to make a config.json file for to be able to input and change the data quick and easily. The code below is my mention.js file that I mapped and referenced into my index.js File. The data below needs to be changed periodically and and a config file that I can set the 1. accountName and 2. userId Into my mention.js file. I’m new to npm and JavaScript and am still learning so any help and advice would be extremely helpful. Thank you!!

const map = new Map(); map.set("Pogo0303PTA",''); map.set("Wretched0671pta", ""); map.set("Pogo0347PTA", ""); map.set("Wretched0047pta", ""); map.set("PogoDroid",""); module.exports = map;``` ```< "acct1": "pogopta000", "userid1": "65465132168451324" >, < "acct2": "userid2": > ETC. 

If you only need to read the config.json file once, you can use const config = require(‘./config.json’) , assuming you have the .js file in the same folder as config.json.

Читайте также:  Свойство object fit css

If you need to read config.json you can use node’s built in module fs by putting const fs = require(‘fs’) at the top of your program and using let config = JSON.parse(fs.readFileSync(‘./config.json’)) to read the file, and using fs.writeFileSync(‘./config.json’, JSON.stringify(config)) to write the file.

You can structure your json file like:

Javascript — Reading a .config file, I would like to have a file such as test.config, and be able to read dbPort, dbHost, and dbName from that .config file in router.js. javascript node.js. Share. Improve this question However JavaScript and JSON go hand in hand. Using a .config would be like using classical inheritance in Js, possible but not …

What is the equivalent of web.config for JavaScript code?

What is the equivalent of the .NET web.config for JavaScript code?

I’m getting to grips with the use of the configuration details in the web.config file, but I don’t know what (or even if) the configuration options are for JavaScript in the page.

The user case I have is how to have a value the JavaScript , in this case a target URL, which can be changed depending upon environment, DEV / TEST / LIVE , without changing the JavaScript code

You could describe a Global JS variable in your master page and use that.

[ Edit ]
In your web.config have

In your master page you would have something like this (I would recommend this done in the controller but for easiness)

  

Then all you would need to do is just change the web.config . I am presuming you are using MVC

How to make and reference a configuration file in, The code below is my mention.js file that I mapped and referenced into my index.js File. The data below needs to be changed periodically and and a config file that I can set the 1. accountName and 2. userId Into my mention.js file. I’m new to npm and JavaScript and am still learning so any help and advice …

Источник

Оцените статью