Giter Site home page Giter Site logo

julianxhokaxhiu / postevent Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 0.0 1.99 MB

A Cross-Domain Event Handler javascript library. Pure Vanilla JS, no dependencies.

License: MIT License

HTML 56.30% JavaScript 43.70%
javascript cross-domain event-handling iframe postmessage

postevent's Introduction

PostEvent

A Cross-Domain Event Handler javascript library. Pure Vanilla JS, no dependencies.

Introduction

This library was born with a simple goal in mind: provide a simple way to handle events, without the effort to code multiple times special code in case we are in a Cross-Domain situation.

Say hello to PostEvent.

Feature set

Cross-Domain

Event handling by default is not possible when an iFrame is not sharing the same origin URL, or the headers are not set to allow Cross-Domain communication.

This library therefore is using postMessage API to enable communication, preserving the same capability of a generic event handling.

Method chain

If you prefer, you can chain methods, in a jQuery-like way-ish. This is an example of method chain:

var pe = new PostEvent();

pe
.on( 'myCustomEvent', function (params){
  console.log( params.foo ); // bar
})
.trigger( 'myCustomEvent', { foo: 'bar' } );

Single channel across multiple instances

You can instanciate as much times as you want the library. By default it is always using the same window.parent entrypoint. This allows you to write simple code, without the need to handle special cases.

var pe1 = new PostEvent(),
    pe2 = new PostEvent();

pe1
.on('sayHello', function (params){
  console.log( 'Hello ' + ( params.name || 'World' ) ); // Hello User
});

p2.trigger('sayHello', { name: 'User' });

UMD Ready

You can use this library in the environment you prefer, thanks to the UMD bundling provided by WebPack.

More info here: https://github.com/umdjs/umd

Install

In order to use this library, you can just include the latest release in your browser like this

<!DOCTYPE html>
<html>
	<head>
		...
	</head>
	<body>
		...
		<script src="dist/PostEvent.js"></script>
		<script>
			var pe = new PostEvent();

			// console.log( pe );
		</script>
	</body>
</html>

or if you prefer as a module

// $ npm i post-event

import PostEvent from 'post-event'

let pe = new PostEvent();
// console.log( pe );

Initialization

When you instanciate the library, you can pass some configuration options, if you prefer:

  • debug: if true it will print some warnings, if the library is misused.
  • namespace: if defined, it will be used to filter messages type. Useful if you want to use multiple instances, with different channels. By default is this class name ( PostEvent )

This is an example of custom initialization:

var pe = new PostEvent({
  debug: true,
  namespace: 'MyCustomNamespace'
});

API

This library is made of two simple APIs:

trigger( name, params )

This API mimics $.trigger() from jQuery. The function accepts two arguments:

  • name: the event name you want to trigger.
  • params: the payload that you want to send.

This is an example usage:

var pe = new PostEvent();

pe.trigger( 'myCustomEvent', { foo: 'bar' } );

on( name, callback )

This API mimics $.on() from jQuery, but it's even more simplier. The function accepts two arguments:

  • name: the event name you want to subscribe.
  • callback: the callback function to call, when the event is detected. params object will be given as an argument, or contextual this object. Both can be null if no parameters were given during trigger.

This is an example of usage:

var pe = new PostEvent();

// Example 1
// No arguments, only this
pe.on( 'myCustomEvent', function (){
  console.log( this.foo ); // bar
});

// Example 2
// One argument, we don't use this
pe.on( 'myCustomEvent', function (params){
  console.log( params.foo ); // bar
});

// Example 3
// One argument, we use also this
pe.on( 'myCustomEvent', function (params){
  console.log( this.foo ); // bar
  console.log( params.foo ); // bar
});

License

See LICENSE

postevent's People

Contributors

dependabot[bot] avatar julianxhokaxhiu avatar steoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

postevent's Issues

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.