微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

backbone.js – 如何在Backbone.Router中使用标准URL?

根据 Backbone.js页面

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的工作原理.或指出我做错了什么.

解决方法

你需要打开pushState:

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/

希望有所帮助.

原文地址:https://www.jb51.cc/js/159804.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐