This is a migrated thread and some comments may be shown as answers.

How to clean up dropdownlist lists

7 Answers 489 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Jaap
Top achievements
Rank 2
Jaap asked on 13 Feb 2012, 02:57 PM
Hi,

I am loading content to my page with ajax calls. This content contains dropdownlists. They create a list DIV in the BODY tag. When the content is updated with a new ajax call, these DIV tags remain in the BODY tag, so they are accumulating there.
How to clean this up? This is especially a problem when you have a one page setup of your application. It is a kind of a 'memory leak' all these DIV tags which will not be used anymore.
Ideally they should not be in the BODY tag, but near the location in the DOM where they are used. But I can imagine that will give trouble with display the list on top of other content.

Ofcourse this applies also for other widgets which have such popups, like e.g. combobox and grid (filter dialogs).

Any thoughts on this housekeeping issue appreciated.

Regards, Jaap

7 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 14 Feb 2012, 04:45 PM
Hello Jaap,

 
You can get the popup element from the corresponding widget and remove it. Something like this:

var popup =  $("#combobox").data("kendoComboBox").popup;
//wrapper will be empty if popup has never been opened
var element = popup.wrapper[0] ? popup.wrapper : popup.element;
 
element.remove(); // remove popup from the DOM
Regards,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jaap
Top achievements
Rank 2
answered on 15 Feb 2012, 03:00 PM
Hi Georgi,

Ok, but my problem is when to call this code?
I have an application setup where I have an container element which gets new content everytime through the jQuery.load ajax method. That load method removes all the content of the container element including the kendo objects contained in the data() object of the elements. But the elements in the body remain.
You need a kind of a dispose method.
I have implemented it now this way:

Just after creation of a widget like a dropdownlist or combobox I add a dispose function to the data object ($ddlSort is my input element on which a kendoDropDownList is created ($ddlSort.kendoDropDownList({...});)
$ddlSort.data('x.dispose', function () { 
var popup = $ddlSort.data("kendoDropDownList").popup;
var element = popup.wrapper[0] ? popup.wrapper : popup.element;
element.remove();
});
and I use this method to switch the content of my container element:
      /* Loads the response from the url in the element and initializes the content */
    $x.loadAndSwitchContent = function ($element, url) {
        /* Dispose oldContent */
        $element.children().find(':hasData(x.dispose)').each(function () {
            $(this).data('x.dispose')();
        });
        $element.empty();
        /* the $element is now empty and will be empty during the request.
        * Would be nicier if we could dispose just before the $element.html is changed.
        * But then we need an own version of the jQuery.load method */
        $element.load(url);
    }

BTW: the :hasData selector is custom selector defined this way:
    /* hasData selector for jQuery */
    $.expr[':'].hasData = function (obj, index, meta, stack) {
        return $.hasData(obj) && ($(obj).data(meta[3]) !== undefined);
    };

Regards, Jaap
0
Accepted
Georgi Krustev
Telerik team
answered on 16 Feb 2012, 05:55 PM
Hello Jaap,

 
I will forward your request regarding the dispose method to our developers for further investigation and consideration. For now you can try the dispose method implemented in this jsFiddle demo. You probably will call it before you try to reinitialize the widget.

All the best,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Bryan Brannon
Top achievements
Rank 2
answered on 27 Feb 2013, 01:34 AM
Please look at this JsFiddle - http://jsfiddle.net/bryanb/bWRTm/1/

I have multiple KendoUI enabled dropdownlists that I need to dispose of.  How can I do that?

Here is what I have tried:  (as shown in the fiddle)
function comboboxDispose() {
    $(".suppliers").each(function () {
        var combobox = $(this).data("kendoComboBox"),
            popup = combobox.popup,
            element = popup.wrapper[0] ? popup.wrapper : popup.element;
 
        //remove popup element;
        element.remove();
 
        //unwrap element
        combobox.element.show().insertBefore(combobox.wrapper);
        combobox.wrapper.remove();
 
        combobox.element.removeData("kendoComboBox");
    });
}
0
Bryan Brannon
Top achievements
Rank 2
answered on 27 Feb 2013, 04:25 AM
I have this figured out.

I (finally) realized I was selecting the wrong elements in my jquery selector.

fixed: http://jsfiddle.net/bryanb/bWRTm/2/
0
Jaap
Top achievements
Rank 2
answered on 27 Feb 2013, 06:29 AM
Hi Bryan,

As far as I know you don't need to implement your own dispose logic.
In the current version of Kendo each widget has it's dispose method.

Regards, Jaap
0
Bryan Brannon
Top achievements
Rank 2
answered on 27 Feb 2013, 01:22 PM
In my testing it seems to be a problem but it might be the way I can using it.

Example:

I have a regular <telerik:RadGrid> that I am using PageMethods to client databind a large collection of data on ready() and then a again at a subsequent interval - every 60 seconds.  Upon success of the RadGrid.set_dataSource() method, I am calling the following to Init() the <input type="text" class="location" /> into a KendoUI ComboBox:
$(input[class='location']).kendoComboBox({
placeholder: "",
dataSource: my_location_json_data
});

The problem I have found is that during my subsequent refreshes... i.e. getting fresh data from my source and calling the set_dataSource() method on my RadGrid (client side) that my memory footprint for the browser grows... and after about 2 hours of refreshes it crashes on a standard configured machine.

I am hoping my newly implemented dispose method will work:

$("input[class='location']").each(function () {
     var combobox = $(this).data("kendoComboBox"),
                    popup = combobox.popup,
                    element = popup.wrapper[0] ? popup.wrapper : popup.element;
      
     //remove popup element
     element.remove();
 
     //unwrap element
     combobox.element.show().insertBefore(combobox.wrapper);
     combobox.wrapper.remove();
      
     combobox.element.removeData("kendoComboBox");
});


-Bryan
Tags
DropDownList
Asked by
Jaap
Top achievements
Rank 2
Answers by
Georgi Krustev
Telerik team
Jaap
Top achievements
Rank 2
Bryan Brannon
Top achievements
Rank 2
Share this question
or