Skip to content

Instantly share code, notes, and snippets.

@olivernn
Created June 26, 2012 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivernn/2994611 to your computer and use it in GitHub Desktop.
Save olivernn/2994611 to your computer and use it in GitHub Desktop.
davis with hash routing
Davis.extend(Davis.hashRouting({ forceHashRouting: true }))
app = Davis(function () {
this.settings.handleRouteNotFound = true
this.get('#/foo', function () {
console.log('foo')
})
this.get('#/bar', function () {
console.log('bar')
})
})
@mirzadelic
Copy link

Thanks for this, but in this case, i don't need hashRouting plugin at all ? It works without that plugin, but is that good way or you can suggest me something else ?

@olivernn
Copy link
Author

Without the hashRouting plugin it will be using pushState, it just so happens that the url you are using also includes a hash. This will work fine in browsers that support the history api, but will fail in browsers that do not.

Personally I think the best approach with Davis is to only use the pushState and have the server also be able to respond to the urls, sometimes this isn't possible though and in those cases you could use hashRouting.

@mirzadelic
Copy link

Then i will use it with hashRouting.
This is my code:

    <script type="text/javascript">
        Davis.extend(Davis.hashRouting({ forceHashRouting: true }))

        var app = Davis(function () {
            this.settings.handleRouteNotFound = true
            this.use(Davis.title);

            this.configure(function () {
                this.generateRequestOnPageLoad = true
            });
            this.get('#/welcome/:name', function (req) {
                console.log("Hello " + req.params['name']);
                this.setTitle(req.params['name']);
            });
        });

        app.start();

    </script>

and now url is: http://localhost/davisjs/##/welcome/oliver with 2 #, what to to with that ?
And second question, there is no ! in url, this is not wrong or ?

@olivernn
Copy link
Author

This is related to the following issue - olivernn/davis.js#44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment