Using director.js, how do I define a default route for the site root which has no # hash in it?

1.8k views Asked by At

I'm using flatiron/director to do client-side routing.

I have routes like:

  var routes = {
    '': function() { loadLandingPage(); },
    '/author': function() { author(); }
  };

If I go to www.example.com#/author, it triggers the author function. If I go to www.example.com# or www.example.com#/ then my landing page is loaded.

However, I want to define a default route for the root of my site without the #. I want the landing page to load even when we land on www.example.com without the #.

I have looked through the documentation, but for the life of me can not figure it out. I thought that the configuration: notfound method would do what I'm thinking, but it did nothing. I tried the following:

var router = Router(routes).configure({ notfound: loadGlobalPage});

and

var router = Router(routes).configure({ notfound: function () {loadGlobalPage();} });

and also after reading this thread, I tried:

router.notfound = function() {
    loadGlobalPage();
};

I also tried a hack unrelated to the router:

function determineIfLandingPage() {
    if (window.location.hash == "") {
        loadGlobalPage();
        window.location.href += "#"
    }
}

This works but it feels like a hack.

This must be a somewhat common use case--how do I get the desired functionality from my router?

1

There are 1 answers

1
towry On

From the document: redirect method will solve this problem.

route.init(['/']) will redirect to the route / if '/#/' is not found in the URL.