使用异步回调反序列化 [英] Deserialize with an async callback

查看:117
本文介绍了使用异步回调反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写了反序列化方法,所以我可以从对应于id的后端加载一个对象。但是,我得到这个对象的方式是异步的。反序列化不等待我的回调并自动返回。例如:

  show:Em.Route.extend({ 
route:/:id,
deserialize:function(router,post){
var postController = router.get('postController');
postController.findById .id,function(post){
return post
});
}

对后端的调用是自动进行反序列化的,是否有一种在反序列化中使用异步调用的方法?



谢谢

解决方案

Luke Melia在这件事上做了一个NYC Ember聚会闪电谈话,使用Promises通过jQuery Deferred对象,基本上,Ember检测到你是否返回承诺(通过查看对象是否具有然后方法),并将路由放在加载状态(您必须声明为反序列化状态的兄弟姐妹)在 ajax.done ,你可以重新解决这个承诺,使路由器完全处于数据完全加载的状态。



使用新的路由器,您实际上不应该使用支持异步过滤器存在于香草状态管理器中,但您可以使用自动加载状态来实现相同的操作。


I'm overriding the deserialize method so I can load an object from the backend corresponding with the id. However, the way I get this object is asynchronous. Deserialize does not wait for my callback and returns automatically.

Example:

show: Em.Route.extend({
        route: "/:id",
        deserialize: function(router, post) {
            var postController = router.get('postController ');
            postController.findById(post.id, function(post) {
                return post
            });
        }

The call to the backend is made but deserialize returns automatically. Is there a way to work with asynchronous call in deserialize?

Thank you

解决方案

Luke Melia did an NYC Ember meetup lightning talk on this very thing, using Promises via jQuery Deferred objects. Basically, Ember detects if you return a Promise (by seeing if the object has a then method), and puts the route in a loading state (which you have to declare as a sibling of the state with the deserialize). On ajax.done, you can resolve the promise, which puts the router fully in the state with the data fully loaded.

With the new router, you're not really supposed to use the support for async transitions that is present in the vanilla statemanager, but you can use the automatic loading states to achieve the same kind of thing.

这篇关于使用异步回调反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆