2014-04-09 43 views
4

下面的示例代碼工作得很好:爲什麼不收集骨幹取返回一個承諾

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 
    } 
}); 

這似乎並不爲集合的工作,雖然。
當我控制檯記錄集合時,我收到一個空集合。

我知道我可以使用方法內的成功回調函數(已經測試過),但我不想這樣做,因爲我想讓函數返回一個可以從其他函數調用的承諾以及。
編輯 - >不,對不起,它似乎不成功的回調工作。

解決方法的任何建議?

編輯;

該圖顯示了從模型和集合獲取方法返回的內容。
除非我做了明顯的錯誤,否則我不明白爲什麼會發生這種情況。
當控制檯記錄成功回調中返回的響應時,我看到填充了屏幕截圖中顯示的空對象。

enter image description here

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; 

}); 
+0

確切位置在哪裏,你得到一個空的收集,以及在什麼回調,你看,它應該包含一些項目實際上? – Bergi

+0

我添加了一個截圖。在jQuery中 - 然後回調。它適用於模型,但是當爲集合使用相同的方法時,它不起作用... – Trace

+1

這是Collection.prototype.fetch返回的結果:'return this.sync('read',this,options) ;''https://github.com/jashkenas/backbone/blob/master/backbone.js#L885-L898反過來,同步'返回Backbone.sync.apply(this,arguments);''反過來返回一個promise 。是否有可能爲此特定集合覆蓋.sync?你能告訴我們該集合的代碼嗎? –

回答

8

實際上,該集合的fetch並返回一個承諾:

代表到骨幹。在自定義持久性策略的封面下同步並返回一個jqXH R.

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

+0

您是否知道collection.fetch()方法返回給我的對象看起來像我添加的屏幕截圖的原因是什麼? – Trace

+2

@KimGysen你已經重寫了'.fetch'或'.sync'這個集合,或者'Backbone.ajax'。 –

+0

我想它和Backbone.ajax沒有關係......否則我猜這個model.fetch可能無法正常工作(?)。你能給我一個關於如何找到問題的指導方針嗎? – Trace