Javascript 将回调函数添加到下划线
我正在使用underline.js的u.invoke()销毁一个集合,下面是代码:Javascript 将回调函数添加到下划线,javascript,underscore.js,Javascript,Underscore.js,我正在使用underline.js的u.invoke()销毁一个集合,下面是代码: _.invoke(Labels.selectedItems(), 'destroy', { error : function (model, response, options) { self.isFailed = true; // initialized with false utilities.activateNotification("error", "Deleting labe
_.invoke(Labels.selectedItems(), 'destroy', {
error : function (model, response, options) {
self.isFailed = true; // initialized with false
utilities.activateNotification("error", "Deleting label failed", response);
}
});
在所有销毁函数完成后,如何添加回调函数(或其他机制?)来检查isFailed的值?我认为您最好看看解决此问题的承诺 但是,这里有一个下划线解决方案,它利用了函数:
// 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();
}