Created
December 1, 2015 15:25
-
-
Save chrisdl/3e1a76da27b97b1bc593 to your computer and use it in GitHub Desktop.
Emitting Events between React and RequireJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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