jquery,kendo内存泄漏问题

时间:2016-09-28 07:59:05

标签: javascript jquery memory-leaks kendo-ui

我对内存泄漏有一些疑问。 请看一下这段代码:

(function ($) {

$.fn.bnftWindow = function (options) {

    // Default Settings
    var settings = $.extend({
    }, options);

    // Validate parameters
    if (settings.id === undefined || settings.id === null || settings.id === "") {
        throw "bnftWindow id option is undefined, null or empty string";
    }


    // If window container div already exists
    if ($("#" + settings.id).length) {
        alert("bnftWindow id already exists - will be destroyed and recreated");
        // destroy window container div
        $("#" + settings.id).remove();
    }

    // create window container div
    $("body").append("<div id='" + settings.id + "'></div>");

    var currentInstance = $("#" + settings.id);

    return currentInstance.each(function () {

        currentInstance.data("bnftWindow", currentInstance);

        var widgetId = currentInstance[0].id; //The id of the html element used to create the bnft widget

        // Apply settings
        $('#' + widgetId).kendoWindow(
            settings
        );

        var dialog = $('#' + widgetId).data("kendoWindow");

        currentInstance.destroy = function () {


            kendo.destroy($("#" + widgetId));

            $("#" + settings.id).remove();
        }
    });



    function onRefresh(e) {

        // Always center window first
        if (settings.refresh !== undefined) {
            settings.refresh(e);
        }

        var dialog = $("#" + settings.id).data("kendoWindow");
        dialog.center();
    }
};

}(jQuery));

这段代码:

function Test()
{
   var element = $("#myElement");
   element.hide();
   var that = this;

   this.ids = { 
      grid: "messagesGrid",
      gridButton: "gridButton",
      composeWnd: "composeWnd"
   };

   this.grid = null; // will store the grid later


        $("#" + this.ids.gridButton).on("click", function () {
            try {
                   // Do something....
                   that.init();
                }
            }
            catch (ex) {
                alert("Error: " + ex);
            }
        });

}

Test.prototype.init = function()
{
   // Do something else...
   var that = this;

   setTimeout(function() {
      that.createWidget();
   }, 500);
}

Test.prototype.createWidget = function()
{
   // Do something else...
   $("#grid").kendoGrid({ // Some properties here });

   // store grid 
   this.grid = $("#grid").data("kendoGrid");
   // Blah blah blah
}
  1. 变量currentInstance,widget id,dialog,element,that,this.grid或导致内存泄漏的事件处理程序?

  2. 如果元素变量导致内存泄漏,则为$(&#34; #myElement&#34;)。hide();解决方案? (除了隐藏后将元素变量设置为null)。

  3. 提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是我清理对话框实例的方法:

    _create: function () {           
        var that = this; 
        ...    
        that.frmElement = that.wrapper.kendoWindow({
           ...
           close: function (e) {
               this.destroy();
               that.wrapper.remove("#" + that.options.dialogId);
               that.wrapper.empty();
            }
            ...
        }).data("kendoWindow");
   }