事件

2012-11-28 21 views
2

在我看來,我有以下綁定:事件

events: 
{ 
    'click #showallconsumers': 'showAllConsumers', 
    'submit form': 'submit', 
    'click #allconsumerstable tbody tr': 'selectConsumer', 
}, 

在showAllConsumers功能,我需要在#showallconsumers錨禁用點擊,獲取收集和重新綁定點擊#showconsumers事件取完之後。

showAllConsumers: function() 
{  
    $(this.el).undelegate('#showallconsumers', 'click'); 

    this.collection.fetch(({async: true, success : this.renderAllConsumers})); 

}, 
renderAllConsumers: function(collection) 
{ 
    //i'm populating table with data from collection 

    $('#showallconsumers').bind('click', this.showAllConsumers); 
}, 

當我點擊#showallconsumers錨,undelegate的作品,但結束時獲取,.bind(...)不能重新綁定事件。我也試過.delegate(...)。

回答

3

fetchsuccess函數沒有以視圖作爲上下文被調用,所以this.showAllConsumers沒有指向你的函數。

當調用fetch包裹success功能Underscore's bind使this點到您的視圖:

this.collection.fetch(({async: true, success : _.bind(this.renderAllConsumers, this)})); 
+0

非常感謝你 –