【发布时间】:2017-03-02 09:11:06
【问题描述】:
我正在开发一个用 AngularJS 编写的电子应用程序。在 Electron 应用程序的主进程中,我正在监听 DownloadItem 事件。因此,每次用户下载文件时,都会触发该事件。在我的 Main.js 中,我有以下代码。
win.webContents.session.on('will-download', (event, item, webContents) => {
item.once('done', (event, state) => {
if (state === 'completed') {
win.webContents.send('download-message', 'completed', item.getSavePath());
} else if (state === 'interrupted') {
win.webContents.send('download-message', 'failed');
}
})
});
在 Angular 方面,我正在“主”控制器(代表应用程序的起点)中等待这样的事件。所以,在这里我有一些看起来像这样的代码:
const { ipcRenderer } = require('electron');
ipcRenderer.on('download-message', function (event, method, filepath) {
if (method === 'completed') {
// Do something interesting
} else if (method === 'failed') {
// Do something interesting
}
});
这很好用,但是在主应用程序控制器中处理所有这些事件感觉有点难看。我想将它分离到它自己的控制器(例如下载控制器)。
我考虑过它在自己的服务(例如 downloadsService)中侦听此事件,但由于我想操纵应用程序的视图(例如显示一些成功/失败消息),我不认为这会是此功能的正确位置。
所以,我正在寻找一种将这个功能分离到特定控制器的好方法。
也许我只是不太了解 Angular 及其控制器的概念,所以任何建议都会很有帮助。
提前致谢!
【问题讨论】:
标签: javascript angularjs events electron