new CustomMethodsApi()
Methods
callCustomMethod(methodName, argument) → {Promise}
Calls a custom method registered in another frame on a page. It returns Promise object so you can wait for a result. If the custom method is registered by several frames on a page then it will be called in all of them. You can still listen to a result but the only one result (a random one) will be delivered to the caller.
customMethodsApi.callCustomMethod('setValue', 'This is a value!').then(
() => console.error('setValue() finished successfully!'),
error => console.error('setValue() failed!', error)
);
customMethodsApi.callCustomMethod('getValue').then(
value => console.log('getValue() finished successfully!', value);
error => console.error('getValue() failed!', error)
)
Parameters:
Name | Type | Description |
---|---|---|
methodName |
string
|
Name of a custom method |
argument |
object
|
Argument for a custom method. It is an object that can be used as a message for window.postMessage() API call (see your browser documentation for any possible limitations) |
Returns:
- Type:
-
Promise
Result of a custom method
registerCustomMethods(methods) → {void}
Registers custom methods so they can be called from other frames on a page. It takes an object with a number of functions. Each function represents a custom method implementation. A function name should match a methodName you use for callCustomMethod API. A function can return either a value or a thenable object. When a value is returned it will lead to resolving a promise on a caller end. When a thenable object is returned we wait until it's resolved/rejected and then propagate the result to a caller.
customMethodsApi.registerCustomMethods({
setValue (newValue) {
return new Promise(function(resolve) {
// set value then resolve the promise
resolve();
});
// or we can simple set a new value
// value = newValue;
}
getValue () {
return new Promise(function(resolve) {
let value;
// get value then resolve the promise
resolve(value);
});
// or we can simple return the value
// return value;
}
});
Parameters:
Name | Type | Description |
---|---|---|
methods |
object
|
Custom methods |
Returns:
- Type:
-
void