Skip to content

Instantly share code, notes, and snippets.

@olivernn
Created June 26, 2012 08:40
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/2994436 to your computer and use it in GitHub Desktop.
Save olivernn/2994436 to your computer and use it in GitHub Desktop.
var app = Davis(function () {
this.configure(function () {
this.linkSelector = 'nav a'
this.formSelector = 'nav form'
})
this.get('/', function (req) {
showHomePage()
})
this.get('/pay-bill', function (req) {
showPayBillPage()
})
this.get('/send-money', function (req) {
showSendMoneyPage()
})
this.get('/contact-us', function (req) {
showContactPage()
})
this.get('/about-us', function (req) {
showAboutPage()
})
this.get('/blog', function (req) {
showBlog()
})
this.post('/search', function (req) {
loadSearchResults(req.params['query'])
})
})
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/pay-bills">Pay Bills</a></li>
<li><a href="/send-money">Send Money</a></li>
<li><a href="/contact-us">Contact Us</a></li>
<li><a href="/about-us">About Us</a></li>
<li><a href="/blog">Blog</a></li>
<li>
<form action="/search" method="post">
<input type="search" name="query" placeholder="Search…"></input>
</form>
</li>
</ul>
</nav>
@olivernn
Copy link
Author

Use relative paths for your links and then for each one that you want to handle with Davis set up a route. The route needs to match on both method and path, so for links that is a get and the path matches the href.

Inside these routes you would do whatever you need when someone clicks on that link. For your use case that might be to render the correct markup in the right place in the page, this is what the show functions might be doing, this part is entirely app specific though.

You also have a post in your navigation and if you wanted to handle this with Davis I have included a matching route. This will pull out the search query and call a function that loads the search results, again this part is entirely app specific.

Assuming that you only want Davis to act on the links and forms inside the nav element I have set the selector that Davis uses for binding to links and clicks to the nav element.

@moduor
Copy link

moduor commented Jun 26, 2012

I will try and let you know.

@moduor
Copy link

moduor commented Jun 26, 2012

Hi Oliver, don't you think I should do a POST so that the server knows how to respond?

@olivernn
Copy link
Author

Which route do you think should be a POST? The search could either be a POST or a GET, it really depends on your app to be honest. I think the navigation links should definitly be GET requests as they are links, but again this is depends on how your app works.

As long as the routes you define in Davis match how the server responds then you should be fine, so if without Davis those navigation links make get requests then they should be get requests in davis too.

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