2016-09-12 7 views
0

私は正常に実行した動的項目を含むトレイメニューを構築しています。唯一の問題は、各アイテムの動的クリックイベントを設定できないことです。私はShellJSを使ってコマンドを実行しています。以下は、私のコードのサンプルです:私のメニュー項目が正常に生成され動的文字列を関数にバインドする電子JS

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 

、唯一の問題は、それが「ファイル[インデックス] [ 『パス』]」の最後の値は、最後の[インデックスを言うことができます使用していますクリックイベントであります] valueは、clickイベントがファイル[3] ['path']の値を使用するたびに、その特定のメニューに対して正しい値(filePath)を使用するように、値を関数にバインドする方法項目がクリックされました。

答えて

1

OK、私は何かを見つけることができなかったインターネットで多くを検索し、私の友人(彼らは何時間も私を助けた)依頼、まだ解決できませんでした。最後に、私は何かを試して、それは働いた。

私の更新されたコード:

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 
関連する問題