raising jquery deferred.then() once all deferred objects have been resolved

痞子三分冷 提交于 2019-11-30 03:55:43

as Eli pointed out, $.when() accepts a comma separated list of arguments and not an array. using Function.apply() to pass in the array seems to do the trick.

function saveAll(callback) {
    var dataArray = [], deferreds = [];
    $.each(dataArray, function() {
        deferreds.push( save() );
    });

    $.when.apply(window, deferreds).then(callback);
}

You should be able to pass a comma-separated list of deferred objects to $.when and .then will execute once they all have resolved.

http://api.jquery.com/jQuery.when/

The problem I think is that $.each is returning your dataArray, not a list of Deferred objects like you want to feed to $.when.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!