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()?
You should bind like this:
$("#id").bind("pluginnameclick", function(){
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With