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

Binding multiple events before initialisation

2 Answers 600 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Max
Top achievements
Rank 1
Max asked on 30 Jun 2020, 12:41 PM

Hi,

 

 

I'm experimenting with the kendo Grid at the moment and have been creating something that creates a grid and sends a callback down a chain when the grid's dataBound event is fired. However, a problem that's arisen is there're multiple callbacks trying to link onto the callback event at different points in the chain, like so:

function createGrid(gridOpt) {
    return new Promise(function (resolve, reject) {
        gridOpt.dataBound = function () { resolve(); };
        $$gridElement.kendoGrid(gridOpt);
    });
}
 
function foo() {
    var gridOpt = getGridOpt();
    createGrid(gridOpt).then(function () {
        console.log('important thing 2');
    });
}
 
function getGridOpt() {
    return {
        dataBound: function () {
            console.log('important call 1');
        },
        //other grid stuff
    };
}

 

However, this would mean that the second dataBound assignation would override the first.

After initialisationyou can bind events through $$gridElem.data('kendoGrid').bind('dataBound', function(){});

I can't use this in this case, as the dataBound event occurs directly after initialisation.

 

One option would simply be to check if there is already an existent function and create a wrapper function that will call both functions. This would work, but it's not exactly best practice, and I'm sure there's a way to do it properly and I just missed something.

Is there any way to bind multiple events to the grid before initialisation?

2 Answers, 1 is accepted

Sort by
0
Accepted
Alex Hajigeorgieva
Telerik team
answered on 02 Jul 2020, 09:48 AM

Hello, Max,

One way you can achieve the desired result is to set the grid autoBind option to false, attach the handlers and then call the grid data source read() method which will bind the grid and invoke the handler that can contain all the functions that need to run:

 grid.bind("dataBound", function(){
     onDataBound1();
     onDataBound2();
});
 grid.dataSource.read();

http://dojo.telerik.com/AGAPELOQ

Let me know what you think.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Max
Top achievements
Rank 1
answered on 02 Jul 2020, 09:53 AM

Hi Alex,

I didn't know about the autoBind option, this will do perfectly, thanks.

Kind Regards,
Max

Tags
General Discussions
Asked by
Max
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Max
Top achievements
Rank 1
Share this question
or