9
votes

How can I get notified before a Backbone router call the specific routing function?

I'd like to have a generic "reset" function before rendering every page.

Is there any event I can bind?

Update: the solutions I found are based on extending the router or the history in order to trigger the event.

4
I have a solution for this issue in this answer: stackoverflow.com/a/16298966/2330244 - Jesús Carrera

4 Answers

9
votes

It looks like the 1.1.x release of Backbone has what you want with the Router.execute method:

MyRouter = Backbone.Router.extend({
  routes: {},

  // fired before every route. 
  execute: function(callback, args) {
    // ...
  }
});
3
votes

if execute function is present it will be called before every route change but you must pass the arguments in callback to properly execute other matching routes.

MyRouter = Backbone.Router.extend({
      routes: {},

      // fired before every route. 
      execute: function(callback, args, name) {
         //your logic
         if (callback) callback.apply(this, args); //this must be called to pass to next route
       },
    });
0
votes

This plugin does what you want. It works with 0.5.3. I'm not certain if it works with 0.9.1 yet or not.

https://github.com/angelo0000/backbone_filters

0
votes

I' am using this in the constructor and it's working fine

this.bind( "all", this.ACL );

Here ACL is a function