Question

I am learning YUI and was wondering what is the best way to access my configurations (stored in a json) using YUI.

One way I came across was to maintain it in the config/app.json and access it with using global variable:
Y.Object.getValue(App, ['Cache', 'globals', 'context'])

Is this the best way? Also if my configuration is spread out over multiple json files, what would be the best way to access them? Thanks

Was it helpful?

Solution

There are basically two ways to do this:

  • Include the configuration in the HTML page
  • Load the configuration using Ajax

Both have some pros and cons.

Including the configuration in the HTML

This requires you to do some server side coding that reads the JSON file and prints it in the page as a global variable. This is what you seem to be doing. The upside of this is that you don't have to make an extra HTTP request. The downside is that you're relying on global variables which can be fragile.

if you're using Node.js you can use express-state to expose that configuration to the client. Alternatively you can use express-yui which relies on a similar mechanism to generate YUI configuration.

Using Ajax

The downside of using Ajax is that it's slower, but the upside is that you can trust the information to be new and not have been modified by anything else in your page.

Dealing with multiple configuration files

My recommendation is that you merge the configuration into a single object. Just decide on some convention for which one wins and generate a single configuration object. This will simplify handling that information in the client. You can do this easily with express-state by just calling app.expose(config1); app.expose(config2) and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top