Author Topic: Call function from listener  (Read 2089 times)

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Call function from listener
« on: November 17, 2016, 01:53:07 pm »
Hi

I would like to call a function from a listener:

listener: function () {             
   call function in the create: function part
}

create: function (evt, ui) {   
   function code   
}

How can I do that?
Thanks.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Call function from listener
« Reply #1 on: November 17, 2016, 07:38:48 pm »
Anonymous functions are commonly used in event callbacks, they are not so convenient to be called at will.

Please use following ways:

1. use an initialization object.

Code: [Select]
        //obj is initialization object.
        var obj = {
width: "80%",
height: 400,
title: "Grid From JSON",
create: function create(){
alert('create');
},
toolbar: {
items:[
{
type:'button',
label:'call create',
listener: function(){
obj.create.call(this);
}
}
]
},
scrollModel: { autoFit: true },
dataModel: { data: data }
        };


2. Abstract the common functionality into a named function and then you can call the functions by their names.

3.
Code: [Select]
listener: function(){
this.option('create').call(this);
}
« Last Edit: November 17, 2016, 08:23:55 pm by paramquery »