事件驱动架构的 jQuery 插件?

发布于 2024-09-04 02:23:20 字数 932 浏览 15 评论 0原文

是否有事件驱动架构 jQuery 插件?

第 1 步:订阅

alt text
订阅者订阅中间的事件处理程序,并传入一个回调方法,以及他们正在侦听的事件的名称...

即两个绿色订阅者将侦听 p0 事件。蓝色订阅者将监听 p1 事件。


步骤 2:p0 事件由事件处理程序的另一个组件触发

alt text

  1. p0 事件被触发事件处理程序
  2. 通知其事件订阅者,调用他们在第 1 步:订阅中订阅时指定的回调方法。

请注意,蓝色订阅者不会收到通知,因为它没有侦听p0 事件。


步骤 3:p1 事件被触发到事件处理程序的组件

alt text

p1 事件被触发另一个组件

与之前一样,只是现在蓝色订阅者通过其回调接收事件,而其他两个绿色订阅者不接收该事件。

Flickr 上 leeand00 的图片

我似乎找不到,但我猜是他们只是在 Javascript/jquery 中将其称为其他名称

另外这种模式有名称吗?因为它不仅仅是一个基本的发布者/订阅者,所以我认为它必须被称为其他名称。

Are there any Event Driven Architecture jQuery plugins?

Step 1: Subscribing

alt text
The subscribers subscribe to the event handler in the middle, and pass in a callback method, as well as the name of the event they are listening for...

i.e. The two green subscribers will be listening for p0 events. And the blue subscriber will be listening for p1 events.


Step 2: The p0 event is fired by another component to the Event Handler

alt text

  1. A p0 event is fired to the Event Handler
  2. The event handler notifies it's subscribers of the event, calling the callback methods they specified when they subscribed in Step 1: Subscribing.

Note that the blue subscriber is not notified because it was not listening for p0 events.


Step 3: The p1 event is fired a component to the Event Handler

alt text

The p1 event is fired by another component

Just as before except that now the blue subscriber receives the event through its callback and the other two green subscribers do not receive the event.

Images by leeand00, on Flickr

I can't seem to find one, but my guess is that they just call it something else in Javascript/jquery

Also is there a name for this pattern? Because it isn't just a basic publisher/subscriber, it has to be called something else I would think.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沉溺在你眼里的海 2024-09-11 02:23:20

您可能不需要插件来执行此操作。首先,DOM 本身完全是事件驱动的。您可以使用事件委托来侦听根节点上的所有事件(jQuery live 使用的技术)。要处理可能与 DOM 无关的自定义事件,您可以使用普通的旧 JavaScript 对象来完成这项工作。我写了一篇关于在 MooTools 中创建中央事件调度程序的 博客文章只需一行代码。

var EventBus = new Class({Implements: Events});

在 jQuery 中也同样容易做到。使用常规 JavaScript 对象作为所有事件的中央代理。任何客户端对象都可以发布和订阅该对象上的事件。请参阅此相关问题

var EventManager = {
    subscribe: function(event, fn) {
        $(this).bind(event, fn);
    },
    unsubscribe: function(event, fn) {
        $(this).unbind(event, fn);
    },
    publish: function(event) {
        $(this).trigger(event);
    }
};

// Your code can publish and subscribe to events as:
EventManager.subscribe("tabClicked", function() {
    // do something
});

EventManager.publish("tabClicked");

EventManager.unsubscribe("tabClicked");

或者,如果您不关心公开 jQuery,则只需使用一个空对象并直接在 jQuery 包装对象上调用 bindtrigger 即可。

var EventManager = {};

$(EventManager).bind("tabClicked", function() {
    // do something
});

$(EventManager).trigger("tabClicked");

$(EventManager).unbind("tabClicked");

包装器只是为了隐藏底层 jQuery 库,以便您可以在以后根据需要替换实现。

这基本上是 发布/订阅观察者模式,Cocoa 的 NSNotificationCenter 类,EventBus 模式由 Ray Ryan 在 GWT 社区和其他几个社区中推广。

You probably don't need a plugin to do this. First of all, the DOM itself is entirely event driven. You can use event delegation to listen to all events on the root node (a technique that jQuery live uses). To handle custom events as well that may not be DOM related, you can use a plain old JavaScript object to do the job. I wrote a blog post about creating a central event dispatcher in MooTools with just one line of code.

var EventBus = new Class({Implements: Events});

It's just as easy to do in jQuery too. Use a regular JavaScript object that acts as a central broker for all events. Any client object can publish and subscribe to events on this object. See this related question.

var EventManager = {
    subscribe: function(event, fn) {
        $(this).bind(event, fn);
    },
    unsubscribe: function(event, fn) {
        $(this).unbind(event, fn);
    },
    publish: function(event) {
        $(this).trigger(event);
    }
};

// Your code can publish and subscribe to events as:
EventManager.subscribe("tabClicked", function() {
    // do something
});

EventManager.publish("tabClicked");

EventManager.unsubscribe("tabClicked");

Or if you don't care about exposing jQuery, then simply use an empty object and call bind and trigger directly on the jQuery wrapped object.

var EventManager = {};

$(EventManager).bind("tabClicked", function() {
    // do something
});

$(EventManager).trigger("tabClicked");

$(EventManager).unbind("tabClicked");

The wrappers are simply there to hide the underlying jQuery library so you can replace the implementation later on, if need be.

This is basically the Publish/Subscribe or the Observer pattern, and some good examples would be Cocoa's NSNotificationCenter class, EventBus pattern popularized by Ray Ryan in the GWT community, and several others.

彩虹直至黑白 2024-09-11 02:23:20

尽管不是 jQuery 插件,Twitter 发布了一个名为 Flight 的 JavaScript 框架,它允许您创建基于组件的架构,通过事件进行通信。

Flight 是 Twitter 的一个轻量级、基于组件的 JavaScript 框架。与其他基于 MVC 模式的 JavaScript 框架不同,Flight 将行为直接映射到 DOM 节点。

Flight 与请求的路由方式或您决定使用哪个模板库无关。 Flight 强制执行严格的关注点分离。飞行中的组件不会直接相互接触。

它们将其操作作为事件进行广播,订阅这些事件的组件可以根据这些事件采取操作。要使用 Flight,您需要 ES5-shim 和 jQuery 以及 AMD 加载程序。

Flight - 来自 Twitter 的轻量级、基于组件的 JavaScript 框架

Though not a jQuery plugin, Twitter released a JavaScript framework called Flight which allows you to create component-based architectures, which communicate via events.

Flight is a lightweight, component-based JavaScript framework from Twitter. Unlike other JavaScript frameworks which are based around the MVC pattern, Flight maps behavior directly to DOM nodes.

Flight is agnostic to how requests are routed or which templating library you decide to use. Flight enforces strict separation of concerns. Components in Flight do not engage each other directly.

They broadcast their actions as events and those components subscribed to those events can take actions based on them. To make use of Flight, you will need the ES5-shim and jQuery along with an AMD loader.

Flight - A Lightweight, Component-Based JavaScript Framework From Twitter

全部不再 2024-09-11 02:23:20

实际上有两个:

There are actually two of them:

一个人练习一个人 2024-09-11 02:23:20

这可以作为轻量级消息传递框架吗?

function MyConstructor() {
    this.MessageQueues = {};

    this.PostMessage = function (Subject) {
        var Queue = this.MessageQueues[Subject];
        if (Queue) return function() {
                                        var i = Queue.length - 1;
                                        do Queue[i]();
                                        while (i--);
                                    }
        }

    this.Listen = function (Subject, Listener) {
        var Queue = this.MessageQueues[Subject] || [];
        (this.MessageQueues[Subject] = Queue).push(Listener);
    }
}

那么你可以这样做:

var myInstance = new MyConstructor();
myInstance.Listen("some message", callback());
myInstance.Listen("some other message", anotherCallback());
myInstance.Listen("some message", yesAnotherCallback());

然后:

myInstance.PostMessage("some message");

将调度队列

Could this serve as a ligthweight message passing framework?

function MyConstructor() {
    this.MessageQueues = {};

    this.PostMessage = function (Subject) {
        var Queue = this.MessageQueues[Subject];
        if (Queue) return function() {
                                        var i = Queue.length - 1;
                                        do Queue[i]();
                                        while (i--);
                                    }
        }

    this.Listen = function (Subject, Listener) {
        var Queue = this.MessageQueues[Subject] || [];
        (this.MessageQueues[Subject] = Queue).push(Listener);
    }
}

then you could do:

var myInstance = new MyConstructor();
myInstance.Listen("some message", callback());
myInstance.Listen("some other message", anotherCallback());
myInstance.Listen("some message", yesAnotherCallback());

and later:

myInstance.PostMessage("some message");

would dispatch the queues

花海 2024-09-11 02:23:20

使用虚拟 jQuery 节点作为调度程序可以轻松完成此操作:

var someModule = (function ($) {

    var dispatcher = $("<div>");

    function init () {
        _doSomething();
    }

    /**
        @private
    */
    function _doSomething () {
        dispatcher.triggerHandler("SOME_CUSTOM_EVENT", [{someEventProperty: 1337}]);
    }

    return {
        dispatcher: dispatcher,
        init: init
    }

}(jQuery));



var someOtherModule = (function ($) {

    function init () {
        someModule.dispatcher.bind("SOME_CUSTOM_EVENT", _handleSomeEvent)
    }

    /**
        @private
    */
    function _handleSomeEvent (e, extra) {
        console.log(extra.someEventProperty) //1337
    }

    return {
        init: init
    }

}(jQuery));

$(function () {
    someOtherModule.init();
    someModule.init();
})

This can easily be accomplished using a dummy jQuery node as a dispatcher:

var someModule = (function ($) {

    var dispatcher = $("<div>");

    function init () {
        _doSomething();
    }

    /**
        @private
    */
    function _doSomething () {
        dispatcher.triggerHandler("SOME_CUSTOM_EVENT", [{someEventProperty: 1337}]);
    }

    return {
        dispatcher: dispatcher,
        init: init
    }

}(jQuery));



var someOtherModule = (function ($) {

    function init () {
        someModule.dispatcher.bind("SOME_CUSTOM_EVENT", _handleSomeEvent)
    }

    /**
        @private
    */
    function _handleSomeEvent (e, extra) {
        console.log(extra.someEventProperty) //1337
    }

    return {
        init: init
    }

}(jQuery));

$(function () {
    someOtherModule.init();
    someModule.init();
})
寂寞清仓 2024-09-11 02:23:20

最近的开发是 msgs.js “面向 JavaScript 的消息编程。受到 Spring Integration 的启发”。它还支持通过 WebSocket 进行通信。

msgs.js 将“企业集成模式”一书中定义的词汇和模式应用于 JavaScript,将面向消息传递的编程扩展到浏览器和/或服务器端 JavaScript。消息传递模式最初是为了集成松散耦合的不同系统而开发的,现在也适用于单个应用程序进程中松散耦合的模块。

[...]

测试环境:

  • Node.js(0.6、0.8)
  • Chrome(稳定)
  • Firefox(稳定,ESR,应该可以在早期版本中使用)
  • IE (6-10)
  • Safari(5、6、iOS 4-6,应该可以在早期版本中使用)
  • Opera(11、12,应该适用于早期版本)

A recent development is msgs.js "Message oriented programming for JavaScript. Inspired by Spring Integration". It also supports communication via WebSockets.

msgs.js applies the vocabulary and patterns defined in the 'Enterprise Integration Patterns' book to JavaScript extending messaging oriented programming into the browser and/or server side JavaScript. Messaging patterns originally developed to integrate loosely coupled disparate systems, apply just as well to loosely coupled modules within a single application process.

[...]

Tested environments:

  • Node.js (0.6, 0.8)
  • Chrome (stable)
  • Firefox (stable, ESR, should work in earlier versions)
  • IE (6-10)
  • Safari (5, 6, iOS 4-6, should work in earlier versions)
  • Opera (11, 12, should work in earlier versions)
柠檬色的秋千 2024-09-11 02:23:20

我已使用 OpenAjax Hub 进行发布/订阅服务。它不是一个 jQuery 插件,而是一个独立的 JavaScript 模块。您可以从 SourceForge 下载并使用参考实现。我喜欢分层主题命名以及使用通配符订阅多个主题的支持。

I have used the OpenAjax Hub for its publish/subscribe services. It's not a jQuery plugin, but a standalone JavaScript module. You can download and use the reference implementation from SourceForge. I like the hierarchical topic naming and the support for subscribing to multiple topics using wildcard notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文