gpt4 book ai didi

javascript - 将动态字符串绑定(bind)到 Electron JS 函数

转载 作者:行者123 更新时间:2023-12-03 06:08:03 25 4
gpt4 key购买 nike

我正在构建一个带有动态项目的托盘菜单,我已经成功完成了。唯一的问题是我无法为每个项目设置动态点击事件。我正在使用 ShellJS 来运行命令。下面是我的代码示例:

var menu = [];
for(index in file) {
menu.push(
{
label: file[index]['name'],
click: function()
{
exec('cd ' + file[index]['path'], function(code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
});
}
}, //SampleCode

我的菜单项已成功生成,唯一的问题是单击事件,它使用“file[index]['path']”的最后一个值,可以说只要有一个,最后一个[index]值就是[3]单击事件它使用 file[3]['path'] 的值,如何将该值绑定(bind)到函数,以便单击事件为单击的特定菜单项使用正确的值(filePath)。

最佳答案

好吧,我在互联网上搜索了很多找不到任何东西,问了我的 friend (他们帮助了我几个小时)仍然无法解决。最后,我尝试了一些方法并且成功了。

我更新的代码:

var menu = [];
for(index in file) {
menu.push(
{
label: file[index]['name'],
id: box[index]['path'], //**Added id parameter**
click: function(currentItem) {

console.log(currentItem.id)
// When click event is triggered it sends
// the current Menu Item as Object
// From that object I can access the 'id'
// example: currentItem.label will give the current items label.
}
}, //Sample Code

关于javascript - 将动态字符串绑定(bind)到 Electron JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39448201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com