Until recently,hash fragments (#page) were used to provide these
permalinks,but with the arrival of the History API,it’s Now
possible to use standard URLs (/page).
我试图添加此路由器规则:
routes: { 'test': function() { alert('ok'); } }
并称为Backbone.history.start({pushState:true,root:’/ myroot /’}).我的页面中有一个链接:
<a href="test">test me</a>
我截获了链接的点击事件:
$('a[href=test]').click(function(e) { router.navigate('test'); e.preventDefault(); });
当我点击链接时,没有发出请求,我相信拦截成功了.但事件并未触发.
所以,请帮助我了解这个History API的工作原理.或指出我做错了什么.
解决方法
Backbone.history.start({pushState:true});
您的链接将强制从您的服务器完全刷新,您的服务器必须使用该URL的内容进行响应.
您需要拦截该链接的点击并告诉您的路由器导航到“测试”路线:
myRouter.navigate( “测试”);
有关HTML5历史记录api:http://diveintohtml5.info/history.html的更多信息
有关使用带有Backbone的pushState的一些介绍级别信息:
http://lostechies.com/derickbailey/2011/09/26/seo-and-accessibility-with-html5-pushstate-part-1-introducing-pushstate/
http://lostechies.com/derickbailey/2011/09/26/seo-and-accessibility-with-html5-pushstate-part-2-progressive-enhancement-with-backbone-js/
我给出的演示视频涵盖了所有这些:
http://lostechies.com/derickbailey/2011/10/06/seo-and-accessibility-with-html5-pushstate-part-3-the-video/
希望有所帮助.