下面的示例代碼工作得很好:爲什麼不收集骨幹取返回一個承諾
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
}
});
這似乎並不爲集合的工作,雖然。
當我控制檯記錄集合時,我收到一個空集合。
我知道我可以使用方法內的成功回調函數(已經測試過),但我不想這樣做,因爲我想讓函數返回一個可以從其他函數調用的承諾以及。
編輯 - >不,對不起,它似乎不成功的回調工作。
解決方法的任何建議?
編輯;
該圖顯示了從模型和集合獲取方法返回的內容。
除非我做了明顯的錯誤,否則我不明白爲什麼會發生這種情況。
當控制檯記錄成功回調中返回的響應時,我看到填充了屏幕截圖中顯示的空對象。
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;
});
確切位置在哪裏,你得到一個空的收集,以及在什麼回調,你看,它應該包含一些項目實際上? – Bergi
我添加了一個截圖。在jQuery中 - 然後回調。它適用於模型,但是當爲集合使用相同的方法時,它不起作用... – Trace
這是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?你能告訴我們該集合的代碼嗎? –