1

I'm trying to write a small library in jQuery and I'd like to add some lifecycle events. I understand that I have to return this; at the end to continue chaining.

Mockup of my library so far:

(function($))
  $.fn.myLibrary = function(){

    // some code here
    return this;

  }
)(jQuery)

This is what I'd like the end result to be:

$('#selector').myLibrary({
    // some options
})
.on('myLibrary-deleting', function(){
    // some code
});

I'm not sure if this question is phrased correctly but how do I extend jQuery's on function?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Arvin Flores
  • 467
  • 2
  • 8
  • 20

1 Answers1

2

You don't need to extend .on() function. Simply trigger your custom even when you need to:

$(this).trigger('myLibrary-deleting')

And .on will keep working.

https://learn.jquery.com/events/introduction-to-custom-events/

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • Within myLibrary, Is there a way to check if the event is being called. So a way to check whether or not .on('myLibrary-deleting') has been chained to myLibrary()? – Arvin Flores Nov 30 '16 at 08:02