In my app I have jquery that is specific to each page, that is scripts that are used strictly on one page. Is there a way to set a scope that I can work within to avoid any id/class collisions or behaviour creeping from one page to another?
I could do this, for example by specifying the wrapper div for the specific page
$(function(){
$('#thing_page input.finder').focus(function(){ ... })
});
but I would need to prepend all the event bindings with the name of the page wrapper, which is a bit annoying.
For those who don’t know, rails compresses all the javascript files in to one, which I like. Otherwise I could just include each file on a specific page.
You can wrap it all in a giant switch statements. Assuming
thing_pageis the id for thebody, you could do this:The syntax highlighting here is pretty bad. Take a look at it in jsfiddle: http://jsfiddle.net/dFuVz/
Alternatively, you might consider using an object to contain your functions (similar to @Andrew’s approach):
Again, since the syntax highlighting here is so terrible, look at it here: http://jsfiddle.net/9L592/