模式从角AJAX调用返回数据 [英] Pattern for returning data from Angular AJAX calls

查看:128
本文介绍了模式从角AJAX调用返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个工厂返回此方法请看下图:

Take a look at this method returned in a factory:

fetchEmployeeList: function() {
    var q = $q.defer();

    $http.get('/Employee/')
         .success(q.resolve)
         .error(ajaxErrorHandler.handleError);

    return q.promise;
}

这code的作者说,这是我们应该从HTTP端点返回数据使用的模型。基本上,任何时候,我们从服务需要的数据,我们应该使用这种模式。作者是IM pression这是pferred超过$返回http.get()的返回值来代替。

The author of this code says that this is the model we should use for returning data from HTTP endpoints. Basically, any time we need data from a service, we should use this pattern. The author is under the impression that this is preferred over returning $http.get()'s return value instead.

我不明白这个code点,虽然。我以为$ http.get()的确实的回报的承诺。

I don't understand the point of this code, though. I thought $http.get() does return a promise.

有人能解释一下这个例子片断是做什么或者他们认为作者可能是想干什么?

Can someone explain what this example snippet is doing or what they think the author might be trying to do?

推荐答案

这就是推迟的反模式,这实际上扭曲时不需要承诺。

That's the deferred anti-pattern, which practically warps a promise when not needed.

在您的情况下返回q.promise似乎是一种滥用,因为HTTP对象可以返回一个承诺本身。

in your case returning a q.promise seems like an abuse since the HTTP object can return a promise itself.

我要重构code以下内容:

I'd refactor the code to the following:

fetchEmployeeList: function() {
    return $http.get('/Employee/');
}

您可以看看这个<一个href=\"http://chariotsolutions.com/blog/post/angularjs-corner-using-promises-q-handle-asynchronous-calls/\"相对=nofollow>博客文章了解参考,以及,

you can take a look to this blog post for more reference as well,

不要害怕打开讨论与谁是建议的方法。

don't be afraid to open a discussion with whoever is suggesting that approach.

这篇关于模式从角AJAX调用返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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