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 7403191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:59:44+00:00 2026-05-29T04:59:44+00:00

I would like to use events to communicate between my objects in a google

  • 0

I would like to use events to communicate between my objects in a google closure (GC) environment.

Suppose I have two classes foobar.Boss and foobar.Employee. The Boss wants to know when the Employee has made coffee, and whether or not that coffee is decaf (he’s laying off the caffeine this week).

GC has made classes available that seem to provide the means to do this, goog.events.Event and goog.events.EventTarget.

Without knowing better, I’d think it would work like this:

foobar.Employee.prototype.makeCoffee = function(isDecaf)
{        
    this.coffeeMaker.putCoffeeInMachine(isDecaf);
    this.coffeeMaker.start();
    var event = new goog.event.Event('COFFEE_ON', { isDecaf: isDecaf });
    goog.events.dispatchEvent(event);
}

foobar.Boss.prototype.addEmployee = function(employee)
{
    ...
    goog.events.listen(employee, 'COFFEE_ON', function(e)
    {
        if (e.target.isDecaf)
        {
            this.refillMug();
        }
    }, false, this);
    ...
}

Is this a correct pattern? I am confused by the class goog.events.EventTarget — how does a target dispatch events? Doesn’t a target have things happen to it?

This question is helpful, but a more direct answer would be appreciated.

  • 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-05-29T04:59:47+00:00Added an answer on May 29, 2026 at 4:59 am

    Having looked at this for a while, my understanding is now that the EventTarget in fact plays a dual roll of being the entity that dispatches events and the entity that is listened to. So one option would be to have Employee inherit goog.events.EventTarget but I have gone a different route.

    First I created a new event type that would let the Boss know if the coffee was decaf.

    /**
     * @constructor
     * @extends {goog.events.Event}
     */
    foobar.CoffeeEvent = function(isDecaf)
    {
      goog.events.Event.call(this, 'COFFEE_ON');
      this.isDecaf = isDecaf;
    };
    goog.inherits(foobar.CoffeeEvent, goog.events.Event);
    

    Next I created an event listener type to dispatch these events.

    /**
     * @constructor
     * @extends {goog.events.EventTarget} 
     */
    foobar.CoffeeEventTarget = function()
    {
      goog.events.EventTarget.call(this);
    };
    goog.inherits(foobar.CoffeeEventTarget, goog.events.EventTarget);
    

    I added an object of this type to my Employee.

    foobar.Employee = function()
    {
      ...
      this.coffeeEvents = new foobar.CoffeeEventTarget();
      ...
    }
    

    When the employee refills the coffee:

    foobar.Employee.prototype.makeCoffee = function(isDecaf)
    {        
        this.coffeeMaker.putCoffeeInMachine(isDecaf);
        this.coffeeMaker.start();
        var event = new foobar.CoffeeEvent(isDecaf);
        this.coffeeEvents.dispatchEvent(event);
    }
    

    Mr. Bossman listens for this.

    foobar.Boss.prototype.addEmployee = function(employee)
    {
        ...
        goog.events.listen(employee.coffeeEvents, 'COFFEE_ON', function(e)
        {
            if (e.isDecaf)
            {
                this.refillMug();
            }
        }, false, this);
        ...
    }
    

    Note that this won’t tell me which employee refilled the coffee, because the event target will an instance of CoffeeEventTarget. If you wanted all of Employee in there I suppose you could add it as a member field. If you were OK with inheriting to Employee from goog.events.EventTarget then you get Employee for free as the target.

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

Sidebar

Related Questions

I have a Drupal 6 web site and would like to use a node
I would like to use CasperJS in node.js. I have referred to the following
I am writing a small wxPython utility. I would like to use some event
I would like to use the OnWorkflowItemChanged event in a WSS 3.0 State machine
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead
I would like to use client-side Javascript to perform a DNS lookup (hostname to

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.