Class as an Event Observer

Posted by emi on Stack Overflow See other posts from Stack Overflow or by emi
Published on 2010-03-04T19:40:33Z Indexed on 2010/04/09 3:03 UTC
Read the original article Hit count: 290

Filed under:
|
|
|
|

I want to do something like this...

var Color = Class.create({
    initialize: function() {
        this._color = "white";
        this.observe("evt: colorChanged", this.colorChanged.bind(this));
    },
    changeColor: function(color) {
        this._color = color;
        this.fire("evt: colorChanged");
    },
    colorChanged: function(event) {
        alert("You change my color!");
    }
});
var a = new Color().changeColor("blue");

Why the colorChange custom event will never be dispatched and I need to use, instead of this, a DOM element like document.observe?

In my code I'd like to know which class dispatches the event using event.target and I can't if I must use document or some other DOM element. :(

I've been working in Actionscript 3 and that's the methodology I learned to work with custom events in classes. What about Javascript?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about custom-events