Pass a function to a factory widget without executing it

I am developing a jQuery widget in jQueryUI factory widgets that need a function that should be passed to it as a parameter. the problem is that the function passed is launched every time a new instance of the widget is created. here is my code:

       $(function(){
            $('a').addrow({
                inputs:[
                    {
                        name:'loo',
                        type:'submit',
                        click:function(){alert(1);}
                    }
                ]
            })

        })

$.widget('namespace.addrow', {
    options: {
        inputs:[]
    },
    _create: function () {
       alert(2);
    }
})

as you can see, I passed an anonymous function in an array inputs, and it alert(1);executes correctly when the document is ready.

+4
source share
1 answer

I don't know about jQueryUI factory widgets, but if your anonymous function gets executed, wrap it in another anonymous function, for example:

click:function(){
  return (function(){
    alert(1);
  }) ;
}

That should work.

update: Without using refund:

click:function(){
  this.click = (function(){
    alert(1);
  }) ;
}
0

All Articles