Go to davis.js routing lib

I am using pushstate / popstate to create a demo, but I am wondering if Davis.js js routing lib, how to use it here, can someone help me for the example below?
Thanks!!

with davis.js index.php

print"<a class=\"a\" href=\"$result[id]\"></a>"; if($_GET['id']){ if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ print" <div class=\"wrapb\"> <div class=\"b\"></div> <div class=\"close\"></div> </div> "; } } 

Js

 var app = Davis(function(){ this.get('?id=:name', function(req){ var b = $(req).filter('.wrapb').html(); $('.a').before(b); $('.close').click(function(){ history.back(); }); }) }) 
-1
source share
1 answer

I think this should do what you are trying to achieve:

 var app = Davis(function () { this.get('/', function (req) { var xhr = $.ajax('/', { data: { id: req.params.id }, dataType: 'html' }) xhr.then(function (data) { var html = $(data) html.find('.close').on('click', function () { history.back() }) $('.a').before(html) }) }) }) 

The req object that is passed to the route handler is not an ajax request, it is an object representing a "request" to the path, in this case / , more of docs .

You need to do whatever work you want to do when the route’s internal feedback is called on the link, in your case it looks like you want to make a request to a server that will respond to the cut html (I'm not a PHP developer, so this may be wrong). Then you want to add this html after the link in the current document.

Davis is not intended for routing based on query parameters, so you should not use them in the path definition, so I changed it to / .

+2
source

Source: https://habr.com/ru/post/911607/


All Articles