开发者

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

开发者 https://www.devze.com 2023-02-03 03:22 出处:网络
I learned how to develop in Javascript using the YUI 2 library and was wondering if there were a jQuery equivalent of Custom Events (http://developer.yahoo.com/yui/event/#customevent)

I learned how to develop in Javascript using the YUI 2 library and was wondering if there were 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 like 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 an 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!


There are lots of pub/sub plugins for jquery.

Example - Rebecca Murphy screencast

Plugins - Ben Almans pubsub, Phiggins pubsub

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号