Giter Site home page Giter Site logo

agrinko / ember-window-messenger Goto Github PK

View Code? Open in Web Editor NEW

This project forked from raido/ember-window-messenger

1.0 1.0 2.0 151 KB

This aims to be a simple window postMessage services provider.

License: MIT License

JavaScript 89.46% HTML 10.37% CSS 0.16%

ember-window-messenger's Introduction

ember-window-messenger

Build Status npm version npm version

This Ember addon is a lightweight postMessage client/server implementation. It is built on promises so the fetch and rpc methods can used directly in your route model() hooks.

For changelog see CHANGELOG.md

It supports JSON only messages for now

Usage

ember install ember-window-messenger

Configuration

Add target:origin map to your config/environment.js. This effectively defines which targets (windows, frames) is your app communicating with.

APP: {
  // Here you can pass flags/options to your application instance
  // when it is created
  'ember-window-messenger': {
    'parent': 'http://localhost:4200',
    'target-1': 'http://localhost:4200',
    'target-2': 'http://localhost:4200',
    'popup': 'http://localhost:4200'
  }
}

This list is also used for validation, to check if message comes from an allowed origin.

Examples

If you dare, fire up the dummy app in this addon and test it out. Below are the basic examples, see dummy app for more.

Setup server on parent

import Ember from 'ember';

export default Ember.Route.extend({
  server: Ember.inject.service('window-messenger-server'),

  init() {
    this._super(...arguments);

    this.get('server').on('demo-data', (resolve, reject, query) => {
      resolve('Some data');
    });
  }
});

Fetch from parent

import Ember from 'ember';

export default Ember.Route.extend({
  client: Ember.inject.service('window-messenger-client'),

  model() {
    return this.get('client').fetch('demo-data');
  }
});

Fetch from a specific target

This can be used from parent window to frames/tabs communication.

import Ember from 'ember';

export default Ember.Route.extend({
  client: Ember.inject.service('window-messenger-client'),

  model() {
    return this.get('client').fetch('popup:demo-data');
  }
});

Execute RPC call

Internally it is the same as fetch, but provides semantic sugar to your app code.

import Ember from 'ember';

export default Ember.Route.extend({
  client: Ember.inject.service('window-messenger-client'),

  actions {
    runMe() {
      this.get('client').rpc('start-worker').then((response) => {
        // handle response here
      });
    }
  }
});

iFrames, popup windows

If you want to communicate with an iframe or a popup window opened with window.open, then you have to register your window instance on the client with matching target name from config/environment map.

iFrame

// app/components/x-frame.js
import Ember from 'ember';

export default Ember.Component.extend({
  client: Ember.inject.service('window-messenger-client'),

  didInsertElement() {
    this.get('client').addTarget('target-1', this.$().get(0).contentWindow);
  },

  willDestroyElement() {
    this.get('client').removeTarget('target-1');
  }
});

Popup with window.open

// app/routes/my-route.js
import Ember from 'ember';

export default Ember.Route.extend({
  client: Ember.inject.service('window-messenger-client'),

  actions: {
    openPopup() {
      let win = window.open('/some/path', 'Example popup', 'toolbar=no,resizable=no,width=400,height=400');
      this.get('client').addTarget('popup', win);
    },

    fetchFromPopup() {
      this.get('client').fetch('popup:some-data').then((name) => {
        this.controller.set('model', name);
      });
    }
  }
});

Open popup if it isn't already open, or has been closed by the user

// app/routes/my-route.js
import Ember from 'ember';

export default Ember.Route.extend({
  client: Ember.inject.service('window-messenger-client'),

  actions: {
    openPopup() {
      if (!this.get('client').hasTarget('popup')) {
        let win = window.open('/some/path', 'Example popup', 'toolbar=no,resizable=no,width=400,height=400');
        this.get('client').addTarget('popup', win);
      }
    },
  }
});

Installation

  • git clone <repository-url> this repository
  • cd ember-window-messenger
  • yarn install or npm install

Running

Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.

ember-window-messenger's People

Contributors

agrinko avatar ember-tomster avatar raido avatar tanihosokawa avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.