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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:11:45+00:00 2026-05-27T17:11:45+00:00

Using Closure Library, you can give any object the ability to dispatch events extending

  • 0

Using Closure Library, you can give any object the ability to dispatch events extending goog.events.EventTarget. Is this currently possible using Dart libraries?

I imagine it would look like this:

#import('dart:html');

class Foo implements EventTarget {
  Events get on() {
    // ???
  }
}

main() {
  Foo foo = new Foo();

  // Subscribe to the event.
  foo.on['bar'].add((Event event) => print('bar!'));

  // Dispatch the event.
  foo.on['bar'].dispatch(new Event('bar'));
}

Am I on the right track?

Edit Thanks to Lars Tackmann, a working draft is here: http://try.dartlang.org/s/f6wk

  • 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-27T17:11:46+00:00Added an answer on May 27, 2026 at 5:11 pm

    You can do this in multiple ways, one method could be to use typedef‘s to define a generic handler function:

    class Event {
       final String type;
       Event(this.type);
    }
    
    typedef EventHandler(Event event);
    
    interface EventTarget {
      Map<String,EventHandler> get on();
      void dispatch(Event event);
    }
    
    class Foo implements EventTarget {
      Map<String, EventHandler> _handlers;
      Foo() {
        _handlers = new Map();
      } 
    
      Map<String, EventHandler> get on() {
        return _handlers;
      }
    
      dispatch(Event event) {
         EventHandler handler = _handlers[event.type];
         handler(event);
      }
    }
    
    main() {
      Foo foo = new Foo();
    
      foo.on['bar'] = (Event event) => print('handling event ${event.type}');
    
      foo.dispatch(new Event('bar'));
    }
    

    I made a DartBoard snippet for you here to play around with.

    You might want to experiment with making EventTarget into a base class, unless of cause it messes up your inheritance strategy (in which case a event bus you can inject with a factory might be a better fit).

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

Sidebar

Related Questions

This solution must use the google closure javascript library and not any other javascript
To quote PHP: Anonymous functions are currently implemented using the Closure class. This is
I am trying to minify a third-party JavaScript library using Google Closure Compiler, but
I'm using closure templates and I can't figure out how to have one main
Using .Net framework 4. I am assuming this log4net version that the PayPal library
I'm calling the twitter4j library using Clojure like so: (def twitter (. (TwitterFactory.) getInstance))
I'm using Closure Compiler to compress and join a few JavaScript files the syntax
I typed this into Clojure REPL (using the enclojure Netbeans plugin): user=> hello, world
I'm using Maven in the context of another build-tool (leiningen for Clojure, but this
Using PyObjC , you can use Python to write Cocoa applications for OS X.

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.