I’m building a Javascript/AJAX heavy web application using jQuery and I’m looking for a way to map URLs/Routes to Javascript functions. I’m using the HTML5 history API and some rewrite rules so all requests will go to one HTML file but idealy what I’d like to do is something along the lines of
Routes.add('/some/path/', 'func_somepath');
Routes.add('/someother/path/', 'func_someotherpath');
function func_somepath(){
alert("You're at somepath");
}
function func_someotherpath(){
alert("You're at someotherpath");
}
Then when someone visited example.com/some/path/ the function ‘func_somepath’ would be called, similar with /someother/path/. It would also be nice to be able to use Rails-style or regexp variables in the URLs
Routes.add('/items/([a-z]+)', 'func_items');
func_items(id){
alert('You requested '+id+'!');
}
Does anything like this already exist or would I have to write it myself? I don’t mind writing it myself but if something already exists there’s no point. I’d also like to avoid using ‘exec’ so how would I go about calling the named functions in Routes.add?
Have you checked out Sinatra‘s JavaScript counter-part, SammyJS? …*ba-dum-tish*