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

is it possible to store existing grid events before unbinding them

2 Answers 180 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 2
Iron
Iron
Veteran
John asked on 26 Aug 2020, 01:48 PM
I have the pleasure of dealing with the code someone else has written that affects every grid on our site (about 100).  
Here is the problem. His default "DataBound" event resizes the action column we have at the left of our grids so that it matches the width of the displayed buttons.  Makes sense.  However, he has code in there that when the grid is intiialized, unbinds the current databound event then rebinds it to his special databound event.  It looks like he assumed that this is the only usage we would ever have for databound.

I discovered this when i created a custom databound on a grid, and it was never called.

Is there a way to modify this code so that it stores the current databound event and reapplies it?

       
gridSelector = gridSelector || "#grid";
 
// scoped function that can be assigned to the dataBoundEvent and called directly (below)
var doGridDataBound = function (me, gridSelector, grid) {
    me.InitMenu(gridSelector);
    setTimeout(function () {
        grid.autoFitColumn(actionColIndex);
    }, 500);
};
 
var grid = $(gridSelector).data("kendoGrid");
// delay "connecting" the dataBound event handler for 250ms to give time for other event handlers to be assigned first (we need this one to "fire" last)
// specifically - customKendoButtons_OnDataBound needs to fire earlier than this one to ensure that the buttons are visible/fully defined before "autoFit" is executed
grid.unbind("dataBound").bind("dataBound", function () {
    doGridDataBound(me, gridSelector, grid);
});
/* if any data rows already exist, then the dataBound event will not fire again, so fire it now */
if ($(gridSelector).find("input.gridPopupHelper").length)
    doGridDataBound(me, gridSelector, grid);

2 Answers, 1 is accepted

Sort by
0
Accepted
Angel Petrov
Telerik team
answered on 28 Aug 2020, 12:51 PM

Hello John,

You can differentiate the handlers and unbind the specific one but for that case one should avoid binding to anonymous functions and passe a reference to the handlers when binding/unbinding. Here is a dojo that illustrates this approach and unbinds a specific handler.

Regards,
Angel Petrov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
John
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 28 Aug 2020, 02:00 PM
thank you.  This is exactly what i needed.
Tags
Grid
Asked by
John
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Angel Petrov
Telerik team
John
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or