Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js force to login route

I'm developing an application where my user needs to login, in this sense logging in is slightly different as the user isn't registered per say. They enter their name, and room name then they connect. The room itself contains all of the settings required for the users such as;

  • Room name
  • View settings - allowing options to be on or off
  • etc...

So what I'm looking to do is hook on to the route event, so I can force the user to go to the /login root if they're not logged in.

Surely there must be a DRY way of doing this without adding a conditional to each route?

like image 540
James Avatar asked Dec 26 '22 12:12

James


1 Answers

Backbone's Router has an execute method that you can override. It is called every time a route is matched, before the callback is called:

var Router = Backbone.Router.extend({
  execute: function(callback, args) {
    // Do what you want here! 
    if (callback) callback.apply(this, args);
  }
});

The method was added with the 1.1.1 release on Feb. 13, 2014.

like image 170
Noah Heldman Avatar answered Jan 31 '23 03:01

Noah Heldman