jQuery-UI对话框内存泄漏 [英] jQuery-UI Dialog Memory Leaks

查看:192
本文介绍了jQuery-UI对话框内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IE7和一些jQuery对话框,我正在运行一个6meg泄漏每个对话框打开。我假设它是与闭包,但到目前为止我做的所有删除它们没有帮助。在这一点上,我想我已经照顾了所有的闭包,除了一个回调函数,我传递,但它仍然泄漏6兆,即使在对话框被关闭和删除。相关源代码是:

I'm working with IE7 and some jQuery dialogs and I'm running into about a 6meg leak per dialog opened. I'm assuming it's to do with closures, but so far everything I've done to remove them haven't helped. At this point I think I've taken care of all the closures except on for a callback function I pass in, but it's still leaking 6 megs even after the dialog is closed and removed. The relevant source code is:

function DialogDestroyAndRemove(event) {
    $(event.target).dialog("destroy").remove();
}

function CallbackAndCloseDialog(event) {
    if (event.data.callback != undefined) {
        event.data.callback(event.data.callbackResponse);
    }
    $("#" + event.data.dialogId).unbind('dialogbeforeclose').dialog('close');
}

// alert dialog modal with ok button
function AlertDialog(dialogTitle, dialogText, callbackFunction) {
    // dynamically generate and add a div so we can create the pop-up
    $('body').append("<div id=\"alertDialog\" style=\"display:none;\" title=\"" + dialogTitle + "\">" + dialogText + "</div>");

    // define/configure the modal pop-up
    $("#alertDialog").dialog({
        draggable: false,
        resizable: false,
        modal: true,
        autoOpen: true,
        open: function() {
            $("#alertDialog").parents('.ui-dialog-buttonpane button:eq(0)')
            .focus() //focus so the button is highlighted by default
            .bind('click', {
                callback: callbackFunction,
                callbackResponse: 'OK',
                dialogId: 'alertDialog'
            }, CallbackAndCloseDialog);
        },
        overlay: { backgroundColor: '#000', opacity: 0.5 },
        buttons: { 'OK': function() { } }
    }).bind('dialogbeforeclose', function(event, ui) {
        // Close (X) button was clicked; NOT the OK button
        if (callbackFunction != undefined) {
            callbackFunction('cancel');
        }
        callbackFunction = null;
    }).bind('dialogclose', DialogDestroyAndRemove);
}

我在上面做了一件事,我不知道是否需要定义OK按钮的回调,当它被定义(因此有一个闭包,因为它引用回调),以定义它使用.bind一旦对话框打开。我希望能够将回调作为数据的一部分传递给点击事件可能有助于删除闭包。

One thing I did above that I'm not sure if it's needed was instead of defining the callback for the OK button when it's defined (and therefore having a closure since it's referencing the callback) to define it using a .bind once the dialog is open. I was hoping that being able to pass the callback as part of the data to the click event might help remove the closure.

任何想法,我可以改变以摆脱这个泄漏?

Any ideas what else I can change to get rid of this leak?

推荐答案

它实际上最终是由于jQuery UI框架如何处理显示背景灰色模态。如果我删除modal = true和重叠属性,内存泄漏下降到〜100k。

It actually ended up being caused by how the jQuery UI framework deals with graying out the background when displaying a modal. If I remove the modal = true and the overlay attributes the memory leak goes down to ~100k.

为了解决这个问题,我不得不使对话框没有模态选项,然后添加一个div自己到页面(固定位置顶部,左边,底部,右边所有0与交替灰色像素,然后透明像素背景),并显示和隐藏与对话框下的zindex。

To get around this I had to make the dialog without the modal option, and then add a div myself to the page (fixed position top, left, bottom, right all 0 with a alternating gray pixel then transparent pixel background) and showing and hiding that with a zindex just under the dialog.

虽然它不是理想的(默认模态叠加是好的和平滑的看),它比每个对话框泄漏那么多的内存,我弹出更好。

While it isn't ideal (the default modal overlay was nice and smooth looking) it's better than leaking that much memory per dialog I pop up.

这篇关于jQuery-UI对话框内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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