My node-webkit application consists of a control window and a presentation window.
The control window gathers data and eventually launches the presentation window via the window.open
function.
The presentation window now has access to some information in the global
variable.
Now I want to render a graphical representation of that information by creating SVG elements and so forth.
I already have a javascript function to do exactly that, but I need some way of starting that function from the control window.
I can't call it directly, since then the function has to access the other window's DOM.
I have tried using the eval
function on the other window's object, but that crashes node-webkit with the message
[18719:0522/205047:ERROR:breakpad_linux.cc(1225)] crash dump file written to
/tmp/chromium-renderer-minidump-788bf8d0c68301d5.dmp
What would be the best way to do this?
Use setInterval
to regularly check a global variable?
One solution I've found to be pretty effective is attaching pub/sub functionality to the global
variable. The setup I've used so far is jQuery-based, though it could also be constructed without. First it is initialized using a variant of this code:
var messages = messages || {};
global.Message = global.Message || function( id ) {
var callbacks, method,
message = id && messages[ id ];
if ( !message ) {
callbacks = jQuery.Callbacks();
message = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove
};
if ( id ) {
messages[ id ] = message;
}
}
return message;
};
Then anywhere between the windows events can be published and subscribed to using the following pattern. One window can publish the data:
global.Message("someButtonClicked").publish(data);
Then the other can listen for it.
global.Message("someButtonClicked").subscribe(onButtonClicked);
function onButtonClicked(data) {
console.log(data);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With