在 collection.fetch 成功回调中重新绑定事件



在我看来,我有以下绑定:

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(...)。

fetchsuccess 函数不会以视图作为其上下文进行调用,因此this.showAllConsumers不会指向您的函数。

调用fetch时,将 success 函数包装在 Underscore 的bind中,以便this指向您的视图:

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

最新更新