如何在 backbone 中调用从 getPage 获取集合的成功回调?

How do I call success callback on fetching collection from getPage in backbone?

在我的代码中,从 getPage 获取集合后,我需要来自 getPage 用于检索数据的获取函数的数据。

collection.getPage(0).done({
    success: function(collection, data, xhr){
        console.log(data);
    },
    error: function(model, response, options){

    }
});

看起来你的 getPage 函数 returns 一个 jQuery Deferred 对象或承诺。

如果是这样,您应该使用 .then 而不是 .done:

function success(data) {
  console.log(data)
}

function error(jqxhr) {
  console.error('oh no!', jqhxr)
}

collection.getPage(0).then(success, error)

$.Deferred.done 接受单个函数参数。它只会在成功时被调用。 .then接受成功回调和错误回调。