aurajs / aura

A scalable, event-driven JavaScript architecture for developing component-based applications.
MIT License
2.94k stars 255 forks source link

Mediator dependency removal Event Emitter #333

Closed ghost closed 7 years ago

ghost commented 10 years ago

@addyosmani @sbellity

We don't need event emitter at all in the mediator. If you want complete library agnostic I would recommend doing this with the $ factory. there should already be a event library in what ever lib you are using and all of them or most of them support custom events even jquery.

The minnowjs script I am going to be putting in this week uses beanjs.

I would do custom events like

$(window).on("CUSTOM_EVENT_NAME", function(data){ console.log(data); });

then the usage is like $(window).trigger("CUSTOM_EVENT_NAME"); or $(window).trigger("CUSTOM_EVENT_NAME", {"someObj": ["one", "two"]});

Jquery is similar but I believe it doesn't work off of window but document should work.

http://www.sitepoint.com/jquery-custom-events/

sbellity commented 10 years ago

Happy to consider it, but the issue is that from our experience, it's extremely useful to have a mediator that allows wildcard subscriptions.

ghost commented 10 years ago

That sounds like functionality you can add to the mediator.

var boundEvents = $.data(document, 'events'); $.each(boundEvents, function () { if (this.indexOf("")) // Checks each event name for an asterisk console.log(this);

// alerts the namespace of the first handler bound to this event name
console.log(this[0].namespace); 

});

atesgoral commented 10 years ago

The prospect of removing the dependence on EE2 and doing the extra logic within the Mediator is exciting because it would lead the way to a '.collect()' implementation within the Mediator as well. See #322.