我正在使用jQuery构建Javascript / AJAX繁重的Web应用程序,并且正在寻找一种将URL /路由映射到Javascript函数的方法。我正在使用HTML5历史记录API和一些重写规则,因此所有请求都将转到一个HTML文件,但理想情况下,我想做的是
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");
}
然后,当有人访问example.com/some/path/时,将调用函数'func_somepath',类似于/ someother / path /。能够在URL中使用Rails样式或regexp变量也很好
Routes.add('/items/([a-z]+)', 'func_items');
func_items(id){
alert('You requested '+id+'!');
}
像这样的东西已经存在了吗,还是我必须自己写?我不介意自己写,但是如果已经存在,那就没有意义了。我也想避免使用'exec',所以我将如何在Routes.add中调用命名函数?
最佳答案