2013-11-21 33 views
0

我正在使用jQuery dataTables來顯示錶。我需要能夠將行選擇事件傳遞給處理選擇的Aura組件,並對來自該行的數據執行一些操作。在AuraJS中獲取組件參考

initialize()功能:

initialize: function() 
{ 
    $("#mytable tbody").click(function(event) 
    { 
     $(mytable.fnSettings().aoData).each(function() 
     { 
     $(this.nTr).removeClass('row_selected'); 
     }); 

     $(event.target.parentNode).addClass('row_selected'); 
    }); 

    mytable = $('#mytable').dataTable(); 
}, 

我設立了行選擇單擊處理程序,但我如何得到一個參考包圍組件,所以我可以sandbox.emit()功能發出的郵件?我可以將組件的引用放入Closure中,但實質上使得此組件成爲單例,並且我不可能同時在頁面上擁有該組件的兩個實例。

是否有一種標準的方式,使用jQuery選擇器或其他方法,我可以從click()處理程序中檢索對封裝組件的引用?

編輯:我不應該嘗試寫代碼,直到我有32盎司的caffine。您可以通過click()方法本身傳遞對當前組件的引用。像這樣:

$("#mytable tbody").click(this, function(event) 
{ 
    $(mytable.fnSettings().aoData).each(function() 
    { 
    $(this.nTr).removeClass('row_selected'); 
    }); 

    $(event.target.parentNode).addClass('row_selected'); 

    event.data.sandbox.emit('mychannel', {data: 'stuff'}); 
}); 

回答

0

如果我正確理解你的問題,你可以嘗試這樣的事情

initialize: function() { 
    var that = this; 
    $("#mytable tbody").click(function(event) { 
     //have acces to component as 'that'  
    }); 
} 

我用什麼事件是視圖組件裏面配置:

View: { 
     events: { 
      'click a[data-question-edit-id]': function (e) { 
       var button = $(e.currentTarget), 
        id = button.attr('data-question-edit-id'), 
        examId = this.component.examModel.get('id'); 

       this.sandbox.router.navigate('/exams/' + examId + '/questions/' + id + '/edit', {trigger: true}); 
      }, 
      'click a[data-question-delete-id]': function (e) { 
       var button = $(e.currentTarget), 
        id = button.attr('data-question-delete-id'); 

       this.component.showDeleteConfirmation(id); 
      } 
     } 
    } 

如果您會發現有幫助,這裏是我正在處理的光環項目的回購: https://github.com/lyubomyr-rudko/aura-test-project