NSNotificationCenter equivalent for reagents?

For some time I was looking for if there were any built-in or any third-party modules to add NSNotificationCenter style functionality to the reaction-based application.

In particular, I want the modules to "listen" for certain types of notifications, and I can "broadcast" events from other parts of the application (from javascript).

The closest thing I found is from 3 days ago: https://stackoverflow.com/a/166269/2126 , but it only supports sending NSNotificationCenter events, not listening.

+4
source share
1 answer

Ok, I figured out an acceptable solution. Here is what I ended up doing if anyone has the same question:

I installed the npm backbone-events-standalone package, which is just the extracted event code from Backbone.js.

In the main entry point for my application ( index.ios.js ), I included the following import code:

 var BackboneEvents = require('backbone-events-standalone'); // global event bus window.EventBus = BackboneEvents.mixin({}); 

Inside any componentDidMount componentDidMount you can now add event listeners, for example:

 componentDidMount() { window.EventBus.on('yourEventName', this.yourEventHandlerFunc); } 

And you can fire events this way:

 window.EventBus.trigger('yourEventName', 'optional event info'); 

It can also be easily combined with NSNotificationCenter events, using something like a solution related in the original question.

If you remove components, it would be wise to also remove event listeners, but I will leave this as an exercise for the reader.

+7
source

All Articles