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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:15:29+00:00 2026-05-31T20:15:29+00:00

With Mootools, you can add a eventListener to an instance of a class. Like

  • 0

With Mootools, you can add a eventListener to an instance of a class.
Like this:

var Widget = new Class({
    Implements: Events,
    initialize: function(element){
    // ...
    },
    complete: function(){
        this.fireEvent('complete');
    }
});

var myWidget = new Widget();
myWidget.addEvent('complete', myFunction);

Is there any posibility to add events on instances link that in prototype and NOT on the document? (Event.observe(document, “evt: eventType”, eventHandler);)

  • 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-31T20:15:31+00:00Added an answer on May 31, 2026 at 8:15 pm

    Prototype’s custom events are tied DOM events so there’s no way to fire events on classes or instances without using the DOM in some way. That said you can use Prototype’s custom events to add custom events to your classes without too much extra work.

    A few years ago Tobie Langel shared a way of adding custom events to Prototype classes; the drawback with this approach is that these events are fired and observed at the class level, meaning events fired by one instance with be picked up all listeners observing any instance of that class.

    I made a few simple modifications to Tobie’s approach, which allow instances to be observed individually. The trick here is using a closure around Class.create to create a private event namespace for each instance of the class. The counter is incremented each time a new instance of the class is created.

    var Observable = Class.create({
      fire : function(eventName, data){
        document.fire(this._namespace + ':' + eventName, data);
      },
      observe : function(eventName, callback){
        document.observe(this._namespace + ':' + eventName, callback);
      },
      stopObserving : function(eventName, callback) {
        // Probably wise to also implement this...
      }
    });
    
    var Widget = (function(){
    
      var namespace = 'widget', counter = 0;
    
      return Class.create(Observable, {
        initialize : function(){
          this._namespace = namespace + ':' + counter++;
        },
        complete : function(){
          this.fire('complete');
        }
      });
    
    }());
    
    var myWidget = new Widget();
    
    myWidget.observe('complete', function(e){
      // Callback executed when 'complete' is fired on myWidget.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using mootools I have a regex like this: new RegExp('^([^\\D'+ separator +']+)(\\d{2})'); In a
Can you do this, like you can with MooTools http://ryanflorence.com/slideshow/#navigation-demo in jQuery? So you
In Mootools, the value of the this variable within a function can be controlled:
In mootools I would do something like $('form_id').send({success:function(res){....}}); What is the parallel syntax in
I have a Mootools class that creates an element. I need to add a
//This is the function that will run every time a new item is added
In prototype you can use the following code. var form = control.form; new Ajax.Updater('result',
I'm a bit new to mootools, but I know in jQuery you can have
It'd be simple to add var myFx = new Fx.Slide(element); to window.addEvent('domready'...) , but
I hope you can help, I am relatively new to mootools (and brand new

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.