Skip to content

Instantly share code, notes, and snippets.

@visiongeist
Last active September 28, 2020 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save visiongeist/a38efc91426787da8648 to your computer and use it in GitHub Desktop.
Save visiongeist/a38efc91426787da8648 to your computer and use it in GitHub Desktop.

AEM 6.2 Authoring Editor to Site messaging API

API

/**
 *
 * @param group {String} group identifier name
 * @param [targetWindow=window.parent]
 * @param [origin='*']
 * @constructor
 */
Granite.author.MessageChannel(group, targetWindow, origin);

/**
 * subscribe to a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.MessageChannel.prototype.subscribeRequestMessage(msg, callback);

/**
 * unsubscribe a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.MessageChannel.prototype.unsubscribeRequestMessage(msg, callback);

/**
 * this function will send a message to the content frame
 *
 * @param msg {String} message identifier
 * @param data {Object} Plain JSON object with data to transfer
 * @param timeout {Number} if the promise should time out,
 *      the default (= 0) is infinite
 *      a negative timeout will not expect any response
 * @return {Promise} null if the timeout is negative
 */
Granite.author.MessageChannel.prototype.postMessage(msg, data, timeout);

/**
 * Add configured functionality to another object
 * @param obj target
 */
Granite.author.MessageChannel.prototype.mixin(obj);

in Authoring context

Editor

/**
 * subscribe to a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.ContentFrame.subscribeRequestMessage(msg, callback);

/**
 * unsubscribe a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.ContentFrame.unsubscribeRequestMessage(msg, callback);

/**
 * this function will send a message to the content frame
 *
 * @param msg {String} message identifier
 * @param data {Object} Plain JSON object with data to transfer
 * @param timeout {Number} if the promise should time out,
 *      the default (= 0) is infinite
 *      a negative timeout will not expect any response
 * @return {Promise} null if the timeout is negative
 */
Granite.author.ContentFrame.postMessage(msg, data, timeout);

Site

/**
 * subscribe to a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.EditorFrame.subscribeRequestMessage(msg, callback);

/**
 * unsubscribe a request message
 *
 * @param msg identifier
 * @param callback
 */
Granite.author.EditorFrame.unsubscribeRequestMessage(msg, callback);

/**
 * this function will send a message to the content frame
 *
 * @param msg {String} message identifier
 * @param data {Object} Plain JSON object with data to transfer
 * @param timeout {Number} if the promise should time out,
 *      the default (= 0) is infinite
 *      a negative timeout will not expect any response
 * @return {Promise} null if the timeout is negative
 */
Granite.author.EditorFrame.postMessage(msg, data, timeout);

Request callback and Response message

/**
 * Function to directly respond to a request.
 * The function call is attached to the request object
 * 
 * @param msg {String} respond message
 * @param [data]
 * @param [error] {String} if error occurs
 */
request.respond(msg, data, error);

e.g.

var mc = new MessageChannel('my-group');
mc.subscribeRequestMessage('a-msg', function (request) {
  request.respond('my-answer', { some: 'data' });
});

Promise resolution

Promise.then(function (result) {
  console.log(result.req); // request
  console.log(result.res); // response
});

Promise.catch(function (result) {
  console.log(result.req); // request
  console.log(result.res); // response - null if a timeout occured
  console.log(result.error); // error message
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment