DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Force page refresh on link click in backbone.js

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.

Upvotes: 1

Views: 1644

Answers (2)

Ahmad Alfy
Ahmad Alfy

Reputation: 13371

It is really about how you attach your events to the <a> tags. The simplest would be to add rel="external" to the links you don't want them to trigger backbone router. Something like:

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

And modify your HTML to be:

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

Upvotes: 2

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14225

I like the solution from backbone-boilerplate project - https://github.com/backbone-boilerplate/backbone-boilerplate/blob/master/app/main.js#L22

Upvotes: 2

Related Questions