是否有 YUI 2 自定义事件发布/订阅事件模型的 jQuery 等效项?
我学习了如何使用 YUI 2 库在 Javascript 中进行开发,并且想知道是否有相当于自定义事件的 jQuery (http://developer.yahoo.com/yui/event/#customevent)
具体来说,我希望能够定义自定义事件,而不必最初附加侦听器。
在 YUI 中,我将创建一个页面类并声明可以订阅的不同自定义事件。下面是一些示例代码来演示我想要做什么,但是使用 jQuery
function ListPage() {
var me = this;
this.initEvent = new YAHOO.util.CustomEvent("initEvent");
this.init = function() {
// initialize events, DOM, etc
this.initEvent.fire(me);
}
}
在应用程序 Javascript 中,我想订阅 initEvent。
var page = new ListPage();
page.initEvent.subscribe(
function (type, args) {
// do stuff here
}
);
page.init();
jQuery 中有类似的教程/示例吗?
我知道我可以使用bind()和trigger()做类似的事情,但我得到的印象是当我调用bind()时我必须传递一个事件处理程序。
是否可以在 jQuery 中创建自定义事件,但稍后传入事件处理程序?
我希望我的问题有意义。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jquery 有很多 pub/sub 插件。
示例 - Rebecca Murphy 截屏
插件- Ben Almans pubsub,Phiggins pubsub
There are lots of pub/sub plugins for jquery.
Example - Rebecca Murphy screencast
Plugins - Ben Almans pubsub, Phiggins pubsub