Backbone.js的和pushState的 [英] Backbone.js and pushState

查看:130
本文介绍了Backbone.js的和pushState的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我中的骨干路由器使pushState的,我需要所有的链接上使用返回false还是骨干自动处理呢?有什么样摆在那里,无论是HTML的一部分和脚本的一部分。

If I enable pushState in the backbone router, do I need to use return false on all links or does backbone handle this automatically? Is there any samples out there, both the html part and the script part.

推荐答案

这是蒂姆Branyen用在他的样板

This is the pattern Tim Branyen uses in his boilerplate:

initializeRouter: function () {
  Backbone.history.start({ pushState: true });
  $(document).on('click', 'a:not([data-bypass])', function (evt) {

    var href = $(this).attr('href');
    var protocol = this.protocol + '//';

    if (href.slice(protocol.length) !== protocol) {
      evt.preventDefault();
      app.router.navigate(href, true);
    }
  });
}

使用,而不是单独做的链接preventDefault,你让路由器在默认情况下处理它们,并通过具有数据旁路属性作出例外。在我的经验,运作良好的模式。我不知道周围的任何伟大的例子,但在尝试它自己应该不会太难。骨干的美在于它的简单性;)

Using that, rather than individually doing preventDefault on links, you let the router handle them by default and make exceptions by having a data-bypass attribute. In my experience it works well as a pattern. I don't know of any great examples around, but trying it out yourself should not be too hard. Backbone's beauty lies in its simplicity ;)

这篇关于Backbone.js的和pushState的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆