1

In my backbone.js app I want to be able to fire a simple link.

 <a href=/logout">Log Out</a>

Now I use it with router like this:

logout: function() {
  window.location = '/logout';
}

Is it possible to call this link straight from the html, just like a normal link? As a notice, the router has pushState enabled.

4

2 回答 2

2

我喜欢骨干样板项目的解决方案 - https://github.com/backbone-boilerplate/backbone-boilerplate/blob/master/app/main.js#L22

于 2013-07-01T08:28:20.860 回答
2

这实际上是关于如何将事件附加到<a>标签。最简单的方法是添加rel="external"到您不希望它们触发骨干路由器的链接。就像是:

$('body').on('click', 'a[href][rel!="external"]', function(e){
    app_router.navigate($(this).attr('href'), {trigger: true});
    e.preventDefault();
});

并将您的 HTML 修改为:

<a href=/logout" rel="external">Log Out</a>
于 2013-07-01T09:06:02.233 回答