简体   繁体   English

骨干路由器导航和锚点href

[英]Backbone Router navigate and anchor href

In a backbone-enabled app, I have seen code that continues to use <a href="#foo"></a> , while the anchor click is handled by a backbone event handler. 在支持骨干的应用程序中,我看到代码继续使用<a href="#foo"></a> ,而锚点击则由骨干事件处理程序处理。

Alternatively, the navigation to #foo can be handled by: 或者,导航到#foo可以通过以下方式处理:

Router.history.navigate("foo");

I believe the latter is the superior approach because it allows easy migration to and from HTML5's pushState functionality. 我相信后者是一种优越的方法,因为它允许轻松迁移到HTML5的pushState功能。 And if we do use pushState, Backbone would be able to gracefully degrade to #foo for browsers that do not support pushState. 如果我们确实使用pushState,那么对于不支持pushState的浏览器,Backbone可以优雅地降级为#foo。

As I am still new to Backbone, can someone more experienced and knowledgable confirm that this is the case? 由于我还是Backbone的新手,有经验和知识渊博的人可以证实这是事实吗?

I personally have pushState enabled and use the approach taken in Tim Branyen's backbone-boilerplate of adding a click handler that sends all link clicks to navigate unless they have a data-bypass attribute: 我个人启用了pushState并使用了Tim Branyen的主干 - 样板中添加点击处理程序的方法 ,该处理程序发送所有链接点击以navigate除非他们具有data-bypass属性:

$(document).on("click", "a:not([data-bypass])", function(evt) {
  var href = { prop: $(this).prop("href"), attr: $(this).attr("href") };
  var root = location.protocol + "//" + location.host + Backbone.history.options.root;

  if (href.prop && href.prop.slice(0, root.length) === root) {
    evt.preventDefault();
    Backbone.history.navigate(href.attr, true);
  }
});

This works pretty well and as @nickf mentions has the advantage that you don't have to use the hash/hashbang hack, even for browsers that do not support pushState. 这非常有效,因为@nickf提到的优点是你不必使用hash / hashbang hack,即使对于不支持pushState的浏览器也是如此。

You should write your links as their "proper" addresses, that is, not with the hash which is just a hack to get around some deficiencies of a particular browser. 你应该把你的链接写成他们的“正确”地址,也就是说,不要用哈希来解决特定浏览器的某些缺陷。 To then make it all work, attach a click handler to catch clicks on these items and pass the urls to Backbone.history, which can then use pushState or convert to a hashbang'd url if needed. 然后让它全部工作,附加一个单击处理程序来捕获这些项目的点击并将URL传递给Backbone.history,然后可以使用pushState或转换为hashbang'd url(如果需要)。 For example: 例如:

$(document).on('click', 'a[href^="/"]', function (event) {
    // here, ensure that it was a left-mouse-button click. middle click should be
    // allowed to pass through
    event.preventDefault();
    Backbone.history.navigate(this.href);
});

Chris' answer is awesome, but there's one addition to it that makes it even better. 克里斯的答案很棒,但是它的一个补充就是让它变得更好。 Backbone.history.navigate() actually returns true or false telling us if it could route to it internally. Backbone.history.navigate()实际上返回true或false,告诉我们它是否可以在内部路由到它。 We can therefore skip the data-bypass attribute and do the following instead: 因此,我们可以跳过data-bypass属性并执行以下操作:

$(document).on("click", "a", function(evt) {
  var href = { prop: $(this).prop("href"), attr: $(this).attr("href") };
  var root = location.protocol + "//" + location.host + Backbone.history.options.root;

  if (href.prop && href.prop.slice(0, root.length) === root) {
    if (Backbone.history.navigate(href.attr, true)) {
      evt.preventDefault();
    }
  }
});

Yes I try to use as much Router.history.navigate as I can in my Backbone apps. 是的,我尝试尽可能多地使用Router.history.navigate在我的Backbone应用程序中。 This also has the benefit of if the user types in the URL "/foo" in their browser, Backbone routes it properly. 这也有好处,如果用户在浏览器中输入URL“/ foo”,Backbone会正确路由它。 Obviously if it is an external link or something you don't want to handle with Backbone then you should not include it. 显然,如果它是一个外部链接或你不想用Backbone处理的东西,那么你不应该包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM