beyond applications and software

JavaScript summed up #5: Configuration values

Maintaining and keeping code clean is always a challenge. And JavaScript is not different here.

In complex applications, JavaScript combines a great deal of application logic with classic GUI concerns. Therefore, temptations are high to put things like labels/texts into code (i.e. hard-coding the values). But already the term “hard-coded” should let you think twice.

A better solution is to keep things like

  • labels / texts
  • URLs
  • configurable values

out of your functions.

Using a single “configuration” object in JavaScript is a simple and comfortable solution to this problem:

MyNamespace.configuration = {
    uris: {
        data_loading_url: 'dummy_data.json',
        details_url: '/projects/###ID###/show'
    },
    labels : {
        date_button: 'Go to date',
    }
}

Using those prepared values in code is then as easy as pie:

var link_element = '<a href="' + MyNamespace.configuration.uris.data_loading_url + '">';
link_element += MyNamespace.configuration.labels.date_button;
link_element += '</a>';

This makes changing those values easy and safe – and separates changeable content (configuration values) from your logic/code.

Share this:
  • Print
  • email
  • PDF
  • Twitter
  • Digg
  • del.icio.us
  • MisterWong
  • StumbleUpon
  • Facebook
  • Netvibes
  • Google Bookmarks
  • FriendFeed
  • Mixx
  • Live
  • Ping.fm
  • Technorati
  • Yigg

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment