【问题标题】:How to update the web profiler toolbar to show data about an ajax request如何更新 Web Profiler 工具栏以显示有关 ajax 请求的数据
【发布时间】:2013-07-15 02:59:30
【问题描述】:

我目前正在构建一个完全支持 ajax 页面加载的应用程序。初始页面加载后,浏览网站仅加载内容,而不加载标题或菜单。

整个应用运行良好,但我想刷新 Web Profiler 工具栏以显示最后的 ajax 请求信息。

我从响应头中获得了 xdebug 令牌,我正在尝试扩展 javascript 部分以替换工具栏的当前内容,但我还没有成功。

我怎样才能做到这一点?有什么需要特别注意的吗?

【问题讨论】:

    标签: javascript jquery symfony


    【解决方案1】:

    我最终“逆向工程”了对象Sfjsload 方法,结果证明它工作得很好。

    解决办法

    // Query the proper URL
    $.get('/my-url', function (data, status, xhr) {
        // Get the xdebugToken from response headers
        var xdebugToken = xhr.getResponseHeader('X-Debug-Token');
    
        // If the Sfjs object exists
        if (typeof Sfjs !== "undefined") {
    
            // Grab the toolbar element
            var currentElement = $('.sf-toolbar')[0];
    
            // Load the data of the given xdebug token into the current toolbar wrapper
            Sfjs.load(currentElement.id, '/_wdt/'+ xdebugToken);
        }
    
    })
    

    【讨论】:

    • 它会取代整个工具栏吗?顺便说一句,仅用于日志,您可以将浏览器控制台(chrome、firefox)与 chromephp、firephp 插件一起使用。
    • 是的,它取代了整个工具栏。应该可以在另一个工具栏上添加一个新工具栏,但它可能会很吵。你说的日志是什么意思? Symfony 日志?
    • jep,从 2.2 开始有 firephp 和 chromephp 日志处理程序,它们在额外的标头中发送完整的日志(来自当前请求和最低日志级别,标准信息)。对于 chrome,有 Chrome Logger,它使您能够在 chrome 控制台中查看日志。还会捕获 ajax 请求。对于 Firefox,需要 Firebug Console。这两个处理程序在config_dev.yml 文件中配置。试试看。
    【解决方案2】:

    我带来了我自己版本的工具栏。
    请看代码的cmets。

    $(function () {
        /**
         * When we make an ajax request, a new debug toolbar is created on top of the previous one, so that we can
         * keep track of every request made to the page.
         *
         * @see http://funktion-it.co.za/2012/12/update-symfony-2-debug-bar-on-each-ajax-call/
         * @see https://gist.github.com/pinano/5677062
         * @see http://stackoverflow.com/questions/17646127/how-to-update-the-web-profiler-toolbar-to-show-data-about-an-ajax-request
         * @param  event
         * @param  XMLHttpRequest
         * @param  ajaxOption
         */
        $(document).ajaxComplete(function (event, XMLHttpRequest, ajaxOption) {
            if (XMLHttpRequest.getResponseHeader('x-debug-token')) {
                // window.location.protocol + '//' + window.location.hostname +
                // the url with the debug token
                var url = '/app_dev.php/_wdt/' + XMLHttpRequest.getResponseHeader('x-debug-token');
                // If the Sfjs object exists
                if (typeof Sfjs !== "undefined") {
                    // create a new id
                    var id = 'sfwdt' + XMLHttpRequest.getResponseHeader('x-debug-token');
                    // when the first ajax call is made
                    if ($('.sf-toolbar').length === 1) {
                        // clone the default debug toolbar
                        var clone = $('.sf-toolbar:eq(0)').clone(true);
                        // change the id of the clone (you cannot have the same id twice)
                        // fmake sure the toolbar containing info about the ajax call
                        // is the one on the bottom
                        clone.attr('id', id).find('.sf-toolbarreset, .sf-minitoolbar').css('bottom', '+=0px');
                        // hide the main toolbar (for improved visual experience)
                        $('.sf-toolbar:eq(0)').find('a.hide-button').click();
                        // and mage sure it will be located on top of the new toolbar
                        $('.sf-toolbar:eq(0)').find('.sf-toolbarreset, .sf-minitoolbar').css('bottom', '+=38px');
                        // append the clone after the main toolbar
                        // append after because you want the main toolbar to
                        // continuously be updated with each ajax call
                        $('.sf-toolbar:eq(0)').after(clone);
                    } else {
                        // if a repeated ajax call
                        // just update the second toolbar (not the default)
                        $('.sf-toolbar:eq(1)').attr('id', id);
                    }
                    // Load the data of the given xdebug token into the current toolbar wrapper
                    Sfjs.load(id, url);
                }
            }
        });
    });

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多