Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zkochan/cross-emitter
EventEmitter style wrapper around postMessage.
https://github.com/zkochan/cross-emitter
Last synced: 10 days ago
JSON representation
EventEmitter style wrapper around postMessage.
- Host: GitHub
- URL: https://github.com/zkochan/cross-emitter
- Owner: zkochan
- License: mit
- Created: 2015-07-24T14:28:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-25T19:48:29.000Z (about 8 years ago)
- Last Synced: 2024-12-01T01:07:01.291Z (22 days ago)
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cross-emitter
EventEmitter style wrapper around postMessage.
[![Dependency Status](https://david-dm.org/zkochan/cross-emitter/status.svg?style=flat)](https://david-dm.org/zkochan/cross-emitter)
[![Build Status](https://travis-ci.org/zkochan/cross-emitter.svg?branch=master)](https://travis-ci.org/zkochan/cross-emitter)
[![npm version](https://badge.fury.io/js/cross-emitter.svg)](http://badge.fury.io/js/cross-emitter)# Installation
```
npm install --save cross-emitter
```## Usage example
Lets say there's a button inside an iframe and it has to emit events to the parent window. Here's how it can be implemented with cross-emitter.
Code inside the iframe:
```js
var CrossEmitter = require('cross-emitter');var innerEmitter = new CrossEmitter({
targets: [
window: window.parent
]
});$('button').click(function() {
innerEmitter.emit('hello', 'Hello world!');
});/* it is also possible to subscribe to events inside the iframe */
innerEmitter.on('hello', function(msg) {
console.log('from iframe: ' + msg);
});/* Emitting events works in both directions.
* It is possible to listen events from the parent window. */
innerEmitter.on('gotHello', function(times) {
console.log('Parent window received hello ', times, ' times');
});
```Code of the parent window:
```js
var CrossEmitter = require('cross-emitter');var outerEmitter = new CrossEmitter({
targets: [
window: targetIframe.contentWindow
]
});var helloesCount = 0;
outerEmitter.on('hello', function(msg) {
console.log(msg);
helloesCount++;
outerEmitter.emit('gotHello', helloesCount);
});
```## License
MIT © [Zoltan Kochan](https://www.kochan.io)