Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ui _trigger vs trigger

I have developed a jquery ui-plugin and can't really understand which of these methods to use. According to the jquery ui documentation I should use _trigger to trigger the events, this allows the handlers to be initialized with the plugin like

$("#id").pluginname({ 
   click: function(){ 
      //called when clicked
   }
});

But if I later want to attach more listeners to this event I can't find any way to do that. I'm trying to use jquery bind, but that does not work. example:

$("#id").bind("click", function(){
    //This does not get fired on click if using _trigger
})

The only solution I have so far is to fire of both, but it feels kind of strange. My code must do the following to work:

$("#id").pluginname({
  click: function(){
     //called when my plugin uses this._trigger('click')
  }
}).bind(function(){
    //called when my plugin uses this.element.trigger('click')
});

I'm using custom events, but didn't think that was relevant for asking this question. Anyone have an idea on how to use event chaining when using _trigger()?

like image 582
Tommy Avatar asked Aug 05 '11 09:08

Tommy


2 Answers

You should bind like this:

$("#id").bind("pluginnameclick", function(){
like image 162
bullgare Avatar answered Oct 03 '22 04:10

bullgare


you should use this._on and this._trigger to do this so that events get cleaned op when destroying the widget.

otherwise you have to unbind the events yourself or otherwise the widget does not get cleaned up by the garbage collection of the browser

hope this answers your question

like image 40
borrel Avatar answered Oct 03 '22 04:10

borrel