Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add provision for before and after hooks in a router #2947

Closed
jaseemabid opened this issue Jan 8, 2014 · 3 comments
Closed

Add provision for before and after hooks in a router #2947

jaseemabid opened this issue Jan 8, 2014 · 3 comments

Comments

@jaseemabid
Copy link

I wanted to execute a specific function before/after every route of a specific controller is matched, and I got here.

http://danialk.github.io/blog/2013/06/08/backbone-tips-after-and-before-methods-for-router/

var Router = Backbone.Router.extend({
    routes: {
        '': function(){
            console.log('INDEX ROUTE');
        }
    },
    before: function () {
        console.log('before');
    },
    after: function () {
        console.log('after');
    }
});

The API looks clean and it does it by monkey patching 2 route callbacks. I think its worth to have this as a core feature. Comments?

@akre54
Copy link
Collaborator

akre54 commented Jan 8, 2014

This has been suggested a few times before (an early one being #299). If you do a search though the issues for similar terms you'll see a bit more background.

It's easy enough to wrap your callbacks directly, with a plugin (i.e. backbone-route-filter), or in a base router pattern. Give it a shot and let me know if that works for you.

@braddunbar
Copy link
Collaborator

Hi @jaseemabid! With the changes in #2890, wrapping up route handlers has been made much easier. I think it will probably fit your needs rather well.

var Router = Backbone.Router.extend({
  execute: function(callback, args) {
    // before...
    if (callback) callback.apply(this, args);
    // after...
  }
});

@jaseemabid
Copy link
Author

@braddunbar That's a good one! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants