oracle-samples / xfc

A javascript library for Cross Frame Communication
Apache License 2.0
17 stars 24 forks source link

Consumer trigger to content provider not functioning #21

Closed poloka closed 6 years ago

poloka commented 6 years ago

Please fill out the below template as best you can.

Description of Issue

Attempted to utilize provided example within project to demonstrate triggering events into the content provider from the consumer. Receive the following error:

error message : Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘http://localprovider.com:8080’) does not match the recipient window’s origin (‘http://localconsumer.com:8080’)

System Configuration

Project Version

https://github.com/cerner/xfc/tree/a74c23f11d033d8cd099a73f588ce30fe6e2d068

Steps to Reproduce the Issue

consumer: https://github.com/prateekgta/xfc/blob/master/example/cross_origin_communication/3_a_index.html provider: https://github.com/prateekgta/xfc/blob/master/example/cross_origin_communication/3_a_provider.html

Expected Outcomes

A message relayed and output in the console that event triggered to the content provider.

kafkahw commented 6 years ago

In your code, try to change this line frame.trigger('pong', {a:1}); to frame.on('xfc.authorized', function(){ this.trigger('pong', {a:1}); });. See what will happen.

Reason: Mounting is an asynchronous process. So when you call XFC.consumer.mount, it returns a frame object (Node's EventEmitter) immediately but the DOM element like iframe is not ready yet. Any trigger call on frame should wait until xfc.authorized event is emitted. The ideal way to handle this is to put your logic in a global handler in init method. For instance,

var globalHandlers = {
  "xfc.authorized": function() 
     this.trigger('hello after authorization', {test: 'greeting'});
  }
};
XFC.Consumer.init(globalHandlers);