当前位置: 动力学知识库 > 问答 > 编程问答 >

Passing Data to Windows in Electron

问题描述:

I'm learning Electron and working with multiple windows and IPC. In my main script I have the following:

var storeWindow = new BrowserWindow({

width: 400,

height: 400,

show: false

});

ipc.on('show-store-edit', function(event, store) {

console.log(store);

storeWindow.loadURL('file://' + __dirname + '/app/store.html');

storeWindow.show();

});

And in my primarily window's script, I have the following inside a click event on a list of stores:

$.getJSON("http://localhost:8080/stores/" + item.id).done(function(store) {

ipc.send('show-store-edit', store);

});

On the console, I am printing the store data from my server. What I'm unclear on is how to get that data into the view for my storeWindow:store.html. I'm not even sure I'm handling the sequence of events correctly but they would be:

  • click Edit Store
  • get store data from server
  • open new window to display store data

or

  • click Edit Store
  • open new window to display store data
  • get store data from server

In the latter, I'm not sure how I would get the ID required to fetch the store from the storeWindow's script.

网友答案:

To send events to particular window you can use webContents.send(EVENT_NAME, ARGS) (see docs). webContents is a property of a window instance:

// main process
storeWindow.webContents.send('store-data', store);

To listen for this event being sent, you need a listener in a window process (renderer):

// renderer process
var ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('store-data', function (store) {
    console.log(store);
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: