Code Monkey home page Code Monkey logo

enhancedpostmessage's Introduction

####This is a pre-release plugin. It has not been tested in a production environment yet.

##EnhancedPostMessage

An easy way to do 1 and 2 way communication across multiple frames/windows. Supports adding events programatically, and triggering events at any time after being instantiated.

Working example can be found at https://truckingsim.github.io/EnhancedPostMessage. These examples show simple 1 way communication, 1 way communication passing data, simple 2 way communication, and 2 way communication passing data to achieve 2 way data-binding between frames.

Usage


Minified and gzipped, EnhancedPostMessage is just over 1KB. To use EnhancedPostMessage you must include the plugin in every frame that will send or listen to post messages. For example, if you have a bookmarklet and that bookmarklet creates an iframe you wish to communicate with, you will need to include EnahncedPostMessage in both the bookmarklet's code (or create a script tag and append it to the body) AND the iframe's source (e.g. in href="/path/to/iframe/html").

The plugin works by using the data given to the trigger, plus your initialization data, to build a meta-data structure that is then sent for the postMessage. This enables custom event name and custom listeners. Due to the custom data structure, the plugin is needed on both sides. If you have multiple sources, e.g., a parent page, an iframe, and a window created with window.open, you will need to include EnhancedPostMessage in each frame. No exceptions.

Any caveats with window.postMessage also apply to EnhancedPostMessage. For example, in IE10 and below, window.postMessage does not work if you create a new window with window.open that is cross origin, however creating a new iframe that is cross-origin is just fine.

This plugin merely leverages existing functionality that is built into most browsers and provides a developer-friendly API. To see if your browser will work with EnhancedPostMessage, please check here.

Any call to any function will initialize the plugin if it hasn't already been so.

Any called to EnhancedPostMessage after initialization with an object will extend the current instance of the plugin. Any events, sources, or listeners with the same name as existing ones will be overwritten on extend.

Options


####stringify

Type: Boolean Default: false Only set to true if you wish to support IE9 and/or IE8. Every other browser can pass objects through postMessage. ####sources Type: Object

List of sources where the value is a single dom element

{
  sources: {
    source1: document.getElementById('test'),
    // If using jQuery you can accomplish the same way using the following
    source2: $('#test')[0]
  }
}

A special internal reserved word of parent is used for sending an event back to the parent frame. No source can use this name.

####events

Type: Object

The events to listen for in a name: value type of way

value - Type: Object|Function|True|String|Number

The value of an event can be an object that is passed to the listener, a function that is called with the data sent from the trigger and a object is returned, or true which means we just trigger the listener on the source with no data passed.

{
  events: {
    exTrue: true,
    exFunc: function(data){
      // do some manipulation to data
      return newData;
    },
    exObj: {
      test: 'data'
    }
  }
}

####listeners

Type: Object

The callbacks to be called on when an event is triggered.

{
  listeners: {
    listener1: function(data){
      // If any data is passed the first param will contain that data.
      //    Otherwise the first param will be `undefined`

      $('#html').html(data);
      // Anything else...
    }
  }
}

There are no required options, and any option can be added at a later time after the plugin has been instantiated through one of the following methods:

Methods


####addEvent

EnhancedPostMessage('addEvent', 'eventName', 'Object|Function|True|String|Number');

####addListener

var FunctionToBeCalled = function(data){};
EnhancedPostMessage('addListener', 'listenerName', FunctionToBeCalled);

####addSource

EnhancedPostMessage('addSource', 'sourceName', document.getElementById('sourceId');

Methods Dynamically


####addEvent

EnhancedPostMessage.addEvent('eventName', 'Object|Function|True|String|Number')

####addListener

var FunctionToBeCalled = function(data){};
EnhancedPostMessage.addListener('listenerName', FunctionToBeCalled);

####addSource

EnhancedPostMessage.addSource('sourceName', document.getElementById('sourceId');

###trigger trigger(event, source, data):

  • event:

Required

Type: String

The name of the event you wish to be triggered

  • source:

Required

Type: String

The name of the source you wish the event to be triggered on

  • data:

Optional

Type: String|Object

Any data you wish to send to the event, if the event is a function. If the event is an object, the event's object will override the data passed. If the event is a bool true then the data will be passed as is to the listener.

Example:

EnhancedPostMessage.trigger('exEvent1', 'source1', {data: {to: {be: 'passed'}}});


## License
Copyright (c) 2015 Adam Heim, contributors.  
Released under the MIT license

enhancedpostmessage's People

Contributors

dcousineau avatar hjc avatar truckingsim avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hjc

enhancedpostmessage's Issues

Make events options

Right now you have to define every event you use. With this change by default will allow any event through and pass any data passed through the trigger, but could also use the predefine for pre-processing.

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.