Node-Webkit启动功能在其他窗口中 [英] Node-Webkit start function in different window

查看:115
本文介绍了Node-Webkit启动功能在其他窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的node-webkit应用程序由一个控制窗口和一个演示窗口组成.

My node-webkit application consists of a control window and a presentation window.

控制窗口收集数据,并最终通过window.open函数启动演示窗口.

The control window gathers data and eventually launches the presentation window via the window.open function.

现在演示窗口可以访问global变量中的某些信息.

The presentation window now has access to some information in the global variable.

现在,我想通过创建SVG元素等来呈现该信息的图形表示.

Now I want to render a graphical representation of that information by creating SVG elements and so forth.

我已经有一个javascript函数可以做到这一点,但是我需要某种方法从控制窗口启动该函数.

I already have a javascript function to do exactly that, but I need some way of starting that function from the control window.

我不能直接调用它,因为此后该函数必须访问另一个窗口的DOM.

I can't call it directly, since then the function has to access the other window's DOM.

我尝试在另一个窗口的对象上使用eval函数,但是这会导致Node-webkit崩溃并显示消息

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?

使用setInterval定期检查全局变量吗?

Use setInterval to regularly check a global variable?

推荐答案

我发现一个非常有效的解决方案是将pub/sub功能附加到global变量.到目前为止,我使用的设置是基于jQuery的,尽管也可以在不使用jQuery的情况下进行构建.首先,使用代码的变体对其进行初始化:

One solution I've found to be pretty effective is attaching pub/sub functionality to the globalvariable. 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;
    };

然后,可以使用以下模式在Windows事件之间的任何位置进行发布和订阅.一个窗口可以发布数据:

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);

然后另一个可以听.

global.Message("someButtonClicked").subscribe(onButtonClicked);

function onButtonClicked(data) {
  console.log(data);
};

这篇关于Node-Webkit启动功能在其他窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆