将回调函数添加到underscorejs _.invoke

时间:2016-03-11 04:07:35

标签: javascript underscore.js

我使用Underscore.js的_.invoke()来销毁一个集合,这里是代码:

 _.invoke(Labels.selectedItems(), 'destroy', {
   error : function (model, response, options) {
     self.isFailed = true; // initialized with false
     utilities.activateNotification("error", "Deleting label failed", response);
   }
 });

如何在完成所有销毁功能后添加回调函数(或其他机制?)来检查isFailed的值?

1 个答案:

答案 0 :(得分:0)

我认为你最好看看解决这个问题的承诺。

然而,这是一个使用after函数的下划线解决方案:

// function will be called after it has been called as many times as there are items
var allDone = _.after(Labels.selectedItems().length, function(){
    console.log('The end');
})

// sample destroy function which calls allDone
var destroy = function(params){
    ....
    allDone();
}