user725995
user725995

Reputation:

Calling .fireEvent() without a context in mootools

Any ideas what the following line does?

What kind of an event is "Popup" here?

fireEvent('Popup','ok');

Upvotes: 1

Views: 1612

Answers (2)

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

Events are typically split into 2 types. DOMEvent and Class.Event.

Most commonly, the latter are of interest. this.fireEvent('popup', 'ok'); will let the instance know something wonderful has happened.

used in conjunction with the [Options,Events] mixins in the Class, if your instance has been created with:

var foo = new someclass({
    onPopup: function(status) {
        console.log(status); // ok!
        console.log(this); // the instance (foo);
    }
});

// later.
foo.fireEvent('popup', 'fail');

But in your case, I am not sure it's about a class, because:

You can also use events in a loose / ambiguous environment like DOM elements where you can fire events to pub/sub various components. eg. window.fireEvent('popup', 'ok'); - or as in your example, fireEvent('popup') on its own - will fire an event callback on the global window object since no other context was defined.

This will work with whatever you have added in a block like so:

window.addEvent('popup', function(status) {

});

NOT typing the context object is really crappy for scope chain lookups as well as readability. the fact that you CAN do just fireEvent('domready') does not mean you should. Whoever wrote that was taking silly shortcuts that don't promote readability. fireEvent itself is an expando property coming from the prototype, it's not a global variable / function, though it looks like one as per the code you posted...

Upvotes: 5

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22441

It, obviously (and according to documentation), fires event of 'Popup' type with argument 'ok'. This seems like custom event type, so to find out what it exactly does and how it interprets it argument, you need to consult source or documentation of code that handles this event.

Upvotes: 2

Related Questions