为什么Backbone Collection fetch不返回承诺
以下示例代码运行良好:
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:
这就是我的collections品:
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-fetch和http://api.jquery.com/jQuery.ajax/#jqXHR