【发布时间】:2014-04-09 15:27:06
【问题描述】:
以下示例代码运行良好:
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
}
});
这似乎不适用于收藏。
当我控制台记录集合时,我得到一个空集合。
我知道我可以在方法中使用成功回调函数(已经测试过),但我不想这样做,因为我希望函数返回一个可以从其他函数调用的承诺好吧。
编辑 -> 不,抱歉,它在成功回调中也不起作用,看起来。
对解决方法有什么建议吗?
编辑;
此图片显示了从模型和集合获取方法返回的内容。
除非我做错了很明显的事情,否则我不明白为什么会发生这种情况。
当控制台在成功回调中记录返回的响应时,我看到屏幕截图中显示的空对象被填充。
编辑2:
这就是我的收藏的样子:
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;
});
【问题讨论】:
-
你从哪里得到一个空集合,在什么回调中你看到它实际上应该包含一些项目?
-
我添加了截图。在 jQuery 中当 - 然后回调。它对模型很有效,但是当对集合使用相同的方法时,它就不起作用了……
-
这是 Collection.prototype.fetch 返回的内容:
return this.sync('read', this, options);github.com/jashkenas/backbone/blob/master/backbone.js#L885-L898 依次执行,同步执行return Backbone.sync.apply(this, arguments);依次返回一个承诺。您是否有可能为这个特定的集合覆盖了 .sync ?你能告诉我们那个集合的代码吗?
标签: javascript jquery backbone.js promise backbone.js-collections