Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:51:40+00:00 2026-06-08T22:51:40+00:00

I’m implementing the module pattern, and would like to know the best/preferred way to

  • 0

I’m implementing the module pattern, and would like to know the best/preferred way to define and register event listeners/handlers. The following works, but maybe there is a better/simpler way…

var  MODULE = function() {

    //  private
    var _field1;
    var _field2;

    function  localFunc(p) {
        alert('localFunc');
    }

    //  public
    return {
        // properties
        prop1: _field1,

        // events
        myEvent1Handler: {},
        myEvent1: function() {myEvent1Handler();},
        myEvent2Handler: {},
        myEvent2: function() {myEvent2Handler();},

        addListener: function  (event,func) {
            if (event  ==  "myEvent1")
                myEvent1Handler = func;   

            if (event  ==  "myEvent2")
                myEvent2Handler = func;      
        },

        // public  methods
        method1: function (p) {
            alert('method1 says:' + p);
            MODULE.myEvent1();
        },
        method2: function  (p) {
             alert('method2 doing  stuff');
             localFunc(p);
            MODULE.myEvent2();
        }

    };
}();

// register for events
MODULE.addListener("myEvent1",function(){alert('fired1');});  
MODULE.addListener("myEvent2",function(){alert('fired2');});  

// use module (only event1 should fire!)
MODULE.method1("hello");  

Try it out:

http://jsfiddle.net/RqusH/3/

Seems like a lot of work to have myEventx, myEventHandlerx, and addListener?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:51:41+00:00Added an answer on June 8, 2026 at 10:51 pm

    Normally, I wouldn’t respond to an architectural question with a specific implementation (especially one dependent on a 3rd-party library like jQuery), but since my implementation promotes reusability — and we are talking patterns here — I will present a small jQuery plugin, $.eventable, which augments JavaScript objects with jQuery’s event methods.

    This plugin will allow you to implement event handling capability on any object (or class instance) with one simple call.

    jQuery.eventable = function (obj) {
      // Allow use of Function.prototype for shorthanding the augmentation of classes
      obj = jQuery.isFunction(obj) ? obj.prototype : obj;
      // Augment the object (or prototype) with eventable methods
      return $.extend(obj, jQuery.eventable.prototype);
    };
    
    jQuery.eventable.prototype = {
    
      // The trigger event must be augmented separately because it requires a
      // new Event to prevent unexpected triggering of a method (and possibly
      // infinite recursion) when the event type matches the method name
      trigger: function (type, data) {
        var event = new jQuery.Event(type); 
        event.preventDefault();                
        jQuery.event.trigger(event, data, this);
        return this;
      }
    };
    
    // Augment the object with jQuery's event methods
    jQuery.each(['bind', 'one', 'unbind', 'on', 'off'], function (i, method) {
      jQuery.eventable.prototype[method] = function (type, data, fn) {
        jQuery(this)[method](type, data, fn);
        return this;
      };
    });
    

    If you include that snippet, you can implement your solution like this:

    var MODULE = function() {
    
      //  private
      var _field1;
      var _field2;
    
      function localFunc(p) {
        alert('localFunc');
      }
    
      //  public
      return $.eventable({
    
        // properties
        prop1: _field1,
    
        // public  methods
        method1: function(p) {
          alert('method1 says:' + p);
          this.trigger('myEvent1');
        },
    
        method2: function(p) {
          alert('method2 doing  stuff');
          localFunc(p);
          this.trigger('myEvent2');
        }
    
      });
    } ();
    
    // register for events
    MODULE.on("myEvent1", function() {
      alert('fired1');
    });
    MODULE.on("myEvent2", function() {
      alert('fired2');
    });
    
    // use module (only event1 should fire!)
    MODULE.method1("hello");
    

    Your MODULE now has the following callable methods:

    MODULE.on(event, /* data, */ handler);
    MODULE.bind(event, /* data, */ handler);
    MODULE.one(event, /* data ,*/ handler);
    MODULE.off(event, handler);
    MODULE.unbind(event, handler);
    MODULE.trigger(event /*, data */);
    

    Where event is a space-delimited list of events, handler is your callback, and data is an optional value to pass to your callbacks.

    You can refer to jQuery’s documentation for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.