Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are common methods of sending initial configuration variables to an Ajax client on the page load?

A complex web-app can have a large amount of user and/or context specific meta and configuration information. eg, user's preferred result-set size, timezone, beginning of the week (Sun/Mon), etc.

What are the possible ways to transport the variables to the client application as it is loading (hidden variables? JSON embedded somewhere?), and what are the pros/cons of each method?

I know of a few ways to hack things together, but I'm curious if anyone has identified good design patterns to use, or best practices they can share.

Although I'd prefer general info on this, my primary stack is jLAMP (jQuery, Apache, MySQL, PHP)

EDIT: I already have applications that do this, so I'm not looking for a quick fix, or suggestions for an entirely different paradigm (as in, not loading config). I'm looking for a discussion with the options and pros/cons of each, so that I might tune what I have, or a new user would be presented with plenty of options to make a good design decision from the start. Thanks!

like image 982
Bryan Agee Avatar asked Sep 27 '11 03:09

Bryan Agee


1 Answers

As others have pointed out, if there is a lot of information to transport during the loading of your application I would not recommend using AJAX, since you're putting an overhead on your connection.

My chosen method would be to render all the configuration information on the head tag of your application, using PHP, that way, once your events get triggered (after the DOM has finished loading I would assume), you have all the information at your disposal.

Code example:

<head>
<script type="text/javascript">
 //Some js code here....
  var myConfigurationName = "<?=$myConfigurationNameFromDatabase;?>";
 //Some more js code here...
</script>
</head>

The way you format your configuration data is completely up to you, it all depends on how related all of it is, or in how many different places / objects / functions you might need to use it.

You could create some different configuration objects depending on your data, or only one that concentrates all of them. It really depends on the application design you have so far.

If you have a strong OO design on your JS code, you could check out this site and see if any of the basic patterns match your needs.

Feel free to elaborate a bit more on your design (if you can) to help us understand where you stand and how we can help you.

Happy coding!

like image 131
Deleteman Avatar answered Sep 23 '22 03:09

Deleteman