Skip to content

Instantly share code, notes, and snippets.

@nickdunn
Created September 6, 2012 11:39
Show Gist options
  • Save nickdunn/3655237 to your computer and use it in GitHub Desktop.
Save nickdunn/3655237 to your computer and use it in GitHub Desktop.
Very basic controller/action based on a page ID
// camelcase body ID e.g. #page-home => home, #page-contact-us => contactUs
var action = $('body').attr('id')
.replace(/^(page-)/, '')
.replace(/^([a-z]{1})/, function(letter) { return letter.toUpperCase(); })
.replace(/-([a-z]{1})/, function(letter) { return letter.replace(/-/,'').toUpperCase(); });
var Controller = {
// #page-home
home: function() {
...
}
// #page-contact-us
contactUs: function() {
...
}
}
if (typeof Controller[action] != 'undefined') {
Controller[action]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment