Skip to content

Instantly share code, notes, and snippets.

@chrisdl
Created December 1, 2015 15:25
Show Gist options
  • Save chrisdl/3e1a76da27b97b1bc593 to your computer and use it in GitHub Desktop.
Save chrisdl/3e1a76da27b97b1bc593 to your computer and use it in GitHub Desktop.
Emitting Events between React and RequireJS
// This kind of sucks because is relies on the window object which means the React will have issues being Isomorphic (rendered server side).
// So if anyone has a better suggestion as to attaching to a global object and waiting for it to exist i'm all ears.
// main.js
requirejs.config({
paths: {
EventEmitter: "vendor/EventEmitter"
},
shim: {
EventEmitter: {
exports: 'EventEmitter'
}
}
});
requirejs(["init"]);
// init.js
define(['EventEmitter'], function(EventEmitter) {
window.EventEmitter = new EventEmitter();
});
// react-file.jsx
var listenerFunction = function (chris, lucy) {
console.log(chris);
console.log(lucy);
};
var addListener = function () {
try {
window.EventEmitter.addListener('listener0', listenerFunction);
window.clearInterval(addListenerInterval);
} catch (e) {
} finally {
}
};
var addListenerInterval = window.setInterval(addListener, 500);
// requirejs-file.js
window.EventEmitter.emitEvent('listener0', ['Chris', 'Lucy'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment