Is there a jQuery Equivalent of YUI 2 Custom Event Publish/Subscribe Event Model?

Posted by Abe on Stack Overflow See other posts from Stack Overflow or by Abe
Published on 2011-01-06T22:43:53Z Indexed on 2011/01/06 22:53 UTC
Read the original article Hit count: 176

Hello!

I learned how to develop in Javascript using the YUI 2 library and was wondering if there is a jQuery equivalent of Custom Events (http://developer.yahoo.com/yui/event/#customevent)

Specifically, I want to be able to define custom events without having to attach the listeners initially.

In YUI, I would create a page class and declare different custom events that can be subscribed to. Below is some example code to demonstrate what I want to do, but with jQuery

function ListPage() {
    var me = this;
    this.initEvent = new YAHOO.util.CustomEvent("initEvent");

    this.init = function() {
         // initialize events, DOM, etc
         this.initEvent.fire(me);
    }
}

In application Javascript, I would then like to subscribe to the initEvent.

var page = new ListPage();
page.initEvent.subscribe(
    function (type, args) {
        // do stuff here
    }
);

page.init();

Are there any tutorials/examples of something this in jQuery?

I understand I can do something similar using bind() and trigger(), but the impression I get is I have to pass in the event handler when I call bind().

Is it possible in jQuery to create the custom event, but pass in the event handler later?

I hope my question makes sense. thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about publish-subscribe