将动态字符串绑定到函数 Electron JS
Bind Dynamic String to Functions Electron JS
我正在构建一个带有动态项目的托盘菜单,我已经成功完成了。唯一的问题是我无法为每个项目设置动态点击事件。我正在使用 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'] 的值,如何将值绑定到函数,以便单击事件为单击的特定菜单项使用正确的值(文件路径)。
好吧,我在网上找了很多东西都没有找到,问了我的朋友(他们帮了我几个小时)仍然没有解决。最后,我尝试了一些东西并且成功了。
我的更新代码:
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
我正在构建一个带有动态项目的托盘菜单,我已经成功完成了。唯一的问题是我无法为每个项目设置动态点击事件。我正在使用 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'] 的值,如何将值绑定到函数,以便单击事件为单击的特定菜单项使用正确的值(文件路径)。
好吧,我在网上找了很多东西都没有找到,问了我的朋友(他们帮了我几个小时)仍然没有解决。最后,我尝试了一些东西并且成功了。
我的更新代码:
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