簡體   English   中英

為什么Backbone Collection fetch不返回承諾

[英]Why doesn't Backbone Collection fetch return a promise

以下示例代碼運行良好:

Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

Auth_controller.prototype.redirect = function(fragment, args, next){ 

    var getAuthStatus = this.isLogged(); 
    var self = this; 

    $.when(getAuthStatus).then(function(response){ 
        //Do something with the response 
    }
}); 

這似乎不適用於Collection。
當我控制台登錄集合時,我得到一個空集合。

我知道我可以在方法中使用成功回調函數(已經測試過),但我不想這樣做,因為我希望函數返回一個我可以從其他函數調用的promise。
編輯 - >不,抱歉它在成功回調中也不起作用。

有關解決方法的任何建議嗎?

編輯;

此圖顯示了從模型和集合獲取方法返回的內容。
除非我做錯了很明顯,否則我不明白為什么會這樣。
當控制台在成功回調中記錄返回的響應時,我看到屏幕截圖中顯示的空對象已填充。

在此輸入圖像描述

EDIT2:

這就是我的收藏品:

define([
  /*--- libraries ---*/
  'jquery',     
  'underscore', 
  'backbone', 

  /*--- model ---*/
  'models/users/role_model'

], function($, _, Backbone, 
                Role_model){

    var Role_collection = Backbone.Collection.extend({ 
        url: '/ingeb/api_v1/users/roles', 
        model: Role_model 
    }); 

    return Role_collection; 

}); 

實際上,集合的fetch確實返回了一個承諾:

代表Backbone.sync負責自定義持久性策略並返回jqXHR。

請參閱http://backbonejs.org/#Collection-fetchhttp://api.jquery.com/jQuery.ajax/#jqXHR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM