更改为davis.js路由库 [英] change to davis.js routing lib

查看:65
本文介绍了更改为davis.js路由库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pushstate/popstate构建了一个演示,但我想知道如果Davis.js js路由库在这里如何使用,有人可以帮我举个例子吗?
谢谢!

I use pushstate/popstate build a demo, but I'm wondering if Davis.js js routing lib how to use here, can anyone help me for below example?
Thanks!!

使用davis.js index.php

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();
            });
    })
})

推荐答案

我认为这应该可以完成您要达到的目标:

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

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)
    })
  })
})

生成给路由处理程序的req对象不是ajax请求,它是表示对路径的请求"的对象,在本例中为/,更多的是

The req object that is yielded to the route handler is not an ajax request, it is an object representing the 'request' to the path, in this case /, more from the docs.

当您在路由回调中单击链接时,您必须做任何您想做的工作,在您的情况下,您似乎想向服务器发出请求,该请求将以一小段html响应(我指的是我不是PHP开发人员,因此这可能是错误的).然后,您想在当前文档中的链接后附加此html.

You have to do any work that you want to happen when a link is clicked inside the route callback, in your case it looks like you want to make a request to the server which will respond with a snipped of html (I'm not a PHP developer so this may be wrong). You then want to append this html after a link in the current document.

Davis的设计不允许基于查询参数进行路由,因此您不应在路径定义中使用它们,因此我将其更改为/.

Davis isn't designed to allow routing based on query params so you shouldn't use them in the path defintion, so I changed it to just /.

这篇关于更改为davis.js路由库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆