npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tienda-nube-remote-page-communication

v1.0.2

Published

JS library that uses WebMessage API for communicating the editor (insta-theme) with the preview (the store itself).

Downloads

6

Readme

remote-page-communication

JS library (npm dependency) that uses WebMessage API for communicating the editor (insta-theme) with the preview (the store itself). It's intended to be used with browserify.


Use

To add the library in another browserify project, just include it in the package.json file this way:

{
	...
	"dependencies": {
		...
		"tienda-nube-remote-page-communication": "https://github.com/TiendaNube/remote-page-communication/archive/v1.0.2.tar.gz",
		...
	}
	...
}

Note that you are also specifying the version.

Then (after npm install) you can include it in your code with the id "tienda-nube-remote-page-communication" this way:

var RemoteCommunication = require('tienda-nube-remote-page-communication');

API

Instantiation methods

The example asumes that you required the library into the RemoteCommunication variable.

Constructor

new RemmoteCommunication(DOMIframe target | window target [, string origin]) -> RemoteCommunication

The library can be instantiated as showed above. You must provide the target parameter and should provide the origin parameter:

  • target: the page you want to communicate with. It can be an iframe DOM element (if you want to communicate with an iframe that's inside of your page) or window.top (if your page is contained into an iframe and you want to communicate with the top window).
  • origin (optional): the allowed origin of the other window. For security reasons, if the other window's origin doesn't match the specified one, then the communication won't occur. Default is "*".

Examples:

var remoteWindow = new RemoteCommunication(document.getElementById('my-iframe'));
var remoteWindow = new RemoteCommunication(window.top, 'tiendanube.com');

Note: all the following examples asume that the variable remoteWindow refers to an instance of the library's constructor (of RemoteCommunication in the example above).

####.ready

.ready(function(RemoteCommunication remoteWindow) callback) -> RemoteCommunication

Attachs a callback that is called when the library is ready (after stablishing the communication with the remote window). The callback is called with a reference to the remote window instance. It returns itself to allow method chaining.

Example:

remoteWindow.ready(function() {
	console.log('The remote window is ready for sending and receiving messages.');
});

Basic messaging methods

####.send

.send(object message) -> RemoteCommunication

Sends the message to the remote window. You don't have to worry about serializing the message. It's serialized internally using the Structured clone algorithm. This method allows method chaining.

Example:

remoteWindow.send({
	foo: 'bar',
	baz: 5
});

####.onMessage

.onMessage(function(object message) callback) -> RemoteCommunication

Attachs a callback that will be called when the remote window sends a message. The callback is called with the message as first parameter. You don't have to worry about deserializing it, because it's deserialized internally using the Structured clone algorithm. This method allows method chaining.

Example:

remoteWindow.onMessage(function(message) {
	console.log('They sent the message', message);
});

Routing methods

The following methods are intended to provide a richer interface for communicating with the remote window

####.get

.get(string path[, object data], function(object data) callback) -> RemoteCommunication

The metaphor of this method is sending a request to a server (which will actually be the remote window). You send the request (with optional data) and specify a callback that will be called by on respose by the remote window.

This method accepts method chaining.

Example:

remote.get('colors', function(colors) {
	// Do magic with the colors that the remote window replied
});

####.post The post method is an alias of the get method.

Altough both methods accepts an optional data parameter, semantically it's expected that the get method will be used without the data parameter and the post method will be used with the data parameter.

Example:

remote.post('colors', ['black', 'white'], function(response) {
	if(response == 'ok') {
		// The remote window says that everything is ok! :)
	}
});

####.route

.route(string path, function(function(object responseMessageData) res, object data) callback) -> RemoteCommunication

The metaphor is setting a route of a REST server: the window will listen with the callback to requests (get or post methods) calls by the remote window on the specified path. The route method sets a "route" on the path parameter that calls the callback function. The callback function is called with two arguments:

  • a res function that you can call to send a message to the remote window. It accepts an object parameter that you can send in response.
  • optional data that the remote window can send when calling the post or get methods.

This method accepts method chaining.

Example:

remoteWindow.route('colors', function(res, data) {
	if(data) {
		// They sent data, they want to set the colours
		setColors(data);
		res('ok');
	}
	else {
		// They didn't send data, they are asking for the default colours
		res(['red', 'green', 'blue']);
	}
});