Search code examples
javascriptsingle-page-applicationsammy.js

How to force initial navigation with SammyJS


I have a SammyJS-based single page application running under http://[mydomain]/[myapp]/[subPath]. The server is configured to return the same HTML "startup" page no matter what [subPath] is, i.e. the route is configured with a wildcard for the subpath.

Since I want users to be able to bookmark a particular subpath in the application (e.g., "/orders/123"), I need to force the client to navigate to that subpath once the "startup" page is loaded. What is the best way to do this? I've tried doing window.location.pathname = window.location.pathname after setting up my application, but that just caused an infinite loop of re-navigating to the page.


Solution

  • When creating the SammyJS application, simply pass window.location.pathname to the run() method as follows:

    Sammy(function () {
        this.get(/* custom route */, function (context) {
                // ... handle navigation ...
        });
    }).run(window.location.pathname); // Voila!