Search code examples
routessammy.js

How can I execute a particular function on every route except one in sammy.js


I have few routes in my application:

Sammy(function() {
    this.get('#!foo', function(){
        ...
    });

    this.get('#!bar', function(){
        ...
    });

    ... and so on...

    this.get('#!special', function(){
        ...
    })
}).run();

And I have a function initialize(); which has no connection to sammy if I hit any of the routes except of #!special. Of course I can copy it each of my routes except the special (which will work, but will be kind of stupid). Is there a way to achieve this?

P.S. or if you do not know how to do this, is there a way to run the function on each route hit?


Solution

  • Perhaps Sammy's .before method would work for you?

    Here's some sample (untested) code that could do what you want:

    this.before({except: {path: '#!special'}}, function() {
        initialize();
    });
    

    The .before() method basically allows you to do some callback before every route, but also allows you to filter out certain routes (or do the opposite and whitelist certain routes).