Binding multiple events before initialisation

3 posts, 0 answers
  1. Max
    Max avatar
    2 posts
    Member since:
    May 2020

    Posted 30 Jun Link to this post

    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. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    995 posts

    Posted 02 Jul Link to this post

    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.
  3. Max
    Max avatar
    2 posts
    Member since:
    May 2020

    Posted 02 Jul in reply to Alex Hajigeorgieva Link to this post

    Hi Alex,

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

    Kind Regards,
    Max

Back to Top