为什么不收集骨干获取回报的承诺 [英] Why doesn't Backbone Collection fetch return a promise
问题描述
下面的例子code效果很好:
Auth_controller.prototype.isLogged =功能(){
//检查用户进行身份验证
变种getAuthStatus = this.auth_model.fetch();
返回getAuthStatus;
};Auth_controller.prototype.redirect =功能(片段,指定参数时,下一个){ 变种getAuthStatus = this.isLogged();
VAR自我=这一点; $。当(getAuthStatus)。然后(功能(响应){
//做一些与响应
}
});
这似乎并不为采集工作虽然。结果
当我控制台日志的收集,我得到一个空回。
我知道我可以在方法(测试已经)中使用成功的回调函数从,但我不想这样做,因为我希望函数返回,我可以从其他功能调用一个承诺好。结果
的修改的 - >没有,对不起它不会成功回调或者看起来是这样的工作。
的解决方法,有什么建议?
Edit(编辑)
这个图像显示了从模型和收集获取方法返回。结果
除非我做错了什么是显而易见的,我不明白为什么会这样。结果
当控制台登录成功回调返回的响应,我认为这是屏幕截图显示的空对象,被填充。
EDIT2:
这是我收集的样子:
定义([
/ * --- ---库* /
jQuery的,
下划线,
'骨干', / * ---型号--- * /
模型/用户/ role_model]函数($,_,骨干,
好榜样){ VAR Role_collection = Backbone.Collection.extend({
网址:'/ ingeb / api_v1 /用户/角色,
型号:Role_model
}); 返回Role_collection;});
事实上,集合的取
并返回一个承诺:
封面定制的持久性策略下,代表们Backbone.sync并返回一个jqXHR。
块引用>请参阅 http://backbonejs.org/#Collection-fetch 并的 http://api.jquery.com/jQuery.ajax/#jqXHR
The following example code works well:
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 } });
This doesn't seem to work for a Collection though.
When I console log the collection, I get an empty collection back.I know that I can use the success callback function from within the method (tested that already), but I don't want to do that, because I want the function to return a promise that I can call from other functions as well.
Edit -> No, sorry it doesn't work in the success callback either so it seems.Any suggestions for a workaround ?
Edit;
This image shows what is returned from the model and collection fetch methods.
Unless I'm doing something wrong that is obvious, I don't understand why this happens.
When console logging the returned response in the success callback, I see that the empty object as shown in the screenshot, gets populated.Edit2:
This is what my collection looks like:
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; });
解决方案Actually, the collection's
fetch
does return a promise:Delegates to Backbone.sync under the covers for custom persistence strategies and returns a jqXHR.
See http://backbonejs.org/#Collection-fetch and http://api.jquery.com/jQuery.ajax/#jqXHR
这篇关于为什么不收集骨干获取回报的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!