在我看来,我有以下绑定:
events:
{
'click #showallconsumers': 'showAllConsumers',
'submit form': 'submit',
'click #allconsumerstable tbody tr': 'selectConsumer',
},
在showAllConsumer函数中,我需要禁用 #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 锚点时,取消委托有效,但是当获取完成后,.bind(...) 无法重新绑定事件。我也尝试了.delegate(...)。
fetch
的 success
函数不会以视图作为其上下文进行调用,因此this.showAllConsumers
不会指向您的函数。
调用fetch
时,将 success
函数包装在 Underscore 的bind
中,以便this
指向您的视图:
this.collection.fetch(({async: true, success : _.bind(this.renderAllConsumers, this)}));