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

  • Home
  • SEARCH
  • 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 8590995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:25:26+00:00 2026-06-11T23:25:26+00:00

I’ve been looking over the internet for examples of implementing the observer pattern in

  • 0

I’ve been looking over the internet for examples of implementing the observer pattern in jquery.

I would like to have it like this

observer1.observe(subject);
observer2.observe(subject);

Define some custom event callbacks for the observers

observer1.bind('customEvent', function(contextData) {
  //Some code
});
observer1.bind('anotherCustomEvent', function(contextData) {
  //Some code  
});
observer2.bind('customEvent', function(contextData) {
  //Some code  
});

The following line would then trigger the customEvent callbacks of both observers

subject.trigger('customEvent', contextData);

while the following would fire anotherCustomEvent on observer1 only, since observer2 doesn’t have that custom event binded

subject.trigger('anotherCustomEvent', contextData);

Guides on the internet is more general:

$( document ).on( "topicName" , function () {
  //..perform some behaviour
});

$( document ).trigger( "topicName" );

(Example from http://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjquery)
I can’t see how the above code can be used to accomplish what I’m looking for.

Either i would have to make it like this (if I keep it like the above example):

$(document).on("customEvent", function () {
  observer1.trigger("customEvent");
  observer2.trigger("customEvent");
});

$(subject).click(function() { 
  $(document).trigger("customEvent");
});

or a little bit better:

$(subject).click(function() { 
  observer1.trigger("customEvent");
  observer2.trigger("customEvent");
});

Either way I’m stuck with having to edit the subject-click-callback or document-customEvent-callback instead of telling the observer to subscribe to the subject.

Have I misunderstood observer pattern or is there a way to achieve what I’m looking for?

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript mention Publish/Subscribe Pattern a bit further down in that chapter. That could be a way for me, but I’m missing the code behind the example.

  • 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-11T23:25:27+00:00Added an answer on June 11, 2026 at 11:25 pm

    From your comment:

    I just can’t see how this is achieved when you have to tell the subject which elements to trigger by selectors instead of the subject having a list that the observer can register to

    Please correct me if I’m wrong, but you seem to be misunderstanding how the pattern was implemented in jQuery. You don’t “tell the subject which elements to trigger”, and the subject doesn’t have “a list that the observer can register to” either. It works like this:

    • The subject/publisher emits/triggers certain events (on certain circumstances you define).
    • The observer/subscriber listens to certain events. It keeps a list of the events it subscribed to.
    • That’s all based on DOM events, so it’s limited by the DOM Event Model.

    For example, consider the following HTML:

    <div id="div1">div 1</div>
    <div id="div2">div 2</div>
    

    Let’s make the inner divs trigger a custom event called 'custom'. You define when that should happen, on this example this will happen when they are clicked:

    $('div').on('click', function(e) {
        $(this).trigger('custom');
    });
    

    Now let’s make the document element subscribe to that custom event:

    $(document).on('custom', function(e) {
        console.log('document is handling custom event triggered by ' + e.target.id);
    });
    

    When the custom event is triggered by one of the divs, the observer/subscriber is notified and a message is logged to the console.

    The example uses document as the observer for a reason: events bubble up the DOM tree, and can only be caught by elements that are ancestors of the one that triggered it. Since document is the root of the DOM tree, it can see all events. If #div1 was our observer, it would only see events triggered by #div1 itself, but not the ones triggered by #div2.

    Maybe that limitation is what confused you?


    There are ways to circumvent that limitation, but usually, if you want to do something to #div1 based upon an event triggered by #div2, you just do it from the callback you have setup on the document element (or the closest common ancestor to both divs). Anyway, it seems you really want an alternative, so here is one in the form of a jQuery plugin:

    $.fn.observe = function(eventName, callback) {
        return this.each(function(){
            var el = this;
            $(document).on(eventName, function(){
                callback.apply(el, arguments);
            })
        });
    }
    

    You can use it like this:

    $('#div1').observe('custom', function(e) {
        // do something
    });
    

    Live example: http://jsfiddle.net/JwJsP/1

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.