views:

30

answers:

2

Hi Brilliant SO Minds,

-- Full disclosure -- this is homework, and this is my capstone project. --

I've written my first big Obj-Oriented Javascript charting application (bar charts, gantt charts, etc) and I'd like to give users the option to customize output -- things like font size, charting colors, etc.

Right now, I'm passing in a config file that contains global variables which are either A) hard-coded, or B) pulling params from the URL. (To be clear, I think its a "config" file -- its just a *.js file with a bunch of globals in in).

My question is this -- is there a better technique for doing this than loading a config file into the global space? What is the "best practice" for this type of thing? Should I have a "settings" object? Or store the settings in an xml file?

Many thanks to everyone that chimes in!

~Daniel

+2  A: 

is there a better technique for doing this than loading a config file into the global space?
Usually, you define your own custom namespace, so your data won't interfere with data defined by any other scripts. Something like

if (!window.my_project) {
    window.my_project = {};

    my_project.SOME_CONFIGURATION_VALUE = 1;
    my_project.some_function = function(){};
    ...
}
Nikita Rybak
A: 

As per Nikita's comment, it may be best to store the settings under a project namespace.

It may also be viable to store the config as JSON and then load it either synchronously or asynchronously -- depending on your preference. This makes it possible for you to maintain your program logic elsewhere without having to have a config file that depends on there being a certain variable to which it must assign an object (i.e. myProj.settings=...). So, for maintainability's sake, program-logic-agnostic JSON settings may be best...

This idea may be overkill though! Just thought it worth putting out there!

J-P