jQuery plugin for Event Driven Architecture?

前端 未结 7 1016
谎友^
谎友^ 2021-01-30 09:00

Are there any Event Driven Architecture jQuery plugins?

Step 1: Subscribing

\"a

相关标签:
7条回答
  • 2021-01-30 09:38

    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.

    0 讨论(0)
  • 2021-01-30 09:41

    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.

    0 讨论(0)
  • 2021-01-30 09:48

    There are actually two of them:

    • Listen (faster): http://plugins.jquery.com/project/Listen
    • Intercept (more advanced): http://plugins.jquery.com/project/Intercept
    0 讨论(0)
  • 2021-01-30 09:50

    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();
    })
    
    0 讨论(0)
  • 2021-01-30 09:51

    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

    0 讨论(0)
  • 2021-01-30 10:00

    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)
    0 讨论(0)
提交回复
热议问题