Mastering Web Application

(Rick Simeone) #1

Communicating with a Back-end Server


Summary


This chapter guided us through different methods of communicating with a back-
end end to retrieve and manipulate data. We've started our journey by looking at the
$http APIs which is the fundamental service for issuing XHR and JSONP request
in AngularJS. Not only we got familiar with the basic APIs of the $http service, but
also we had a close look at different ways of dealing with the cross-origin requests.


Many of the AngularJS asynchronous services rely on the Promise API to provide
elegant interfaces. The $http service heavily depends on promises so we had to
exhaustively cover promises implementation in AngularJS. We saw that the $q
service provides a general purpose Promise API, and is tightly integrated with the
rendering machinery. Having a good understanding of the Promise API with $q
allowed us to fully understand values returned from the $http method calls.


AngularJS can easily communicate with RESTful endpoints. There is the dedicated
$resource factory that makes it very easy write code interacting with RESTful
back-ends. The $resource factory is very convenient but it is very generic, and as
such might not cover all your needs. We shouldn't shy away from creating custom
$resource-like factories based on the $http API.


Towards the end of the chapter, we've covered some of the advanced usages of the
$http API with response interceptors.


Lastly, as any JavaScript code, methods using the $http APIs should be thoroughly
tested, and we saw that AngularJS provides excellent mock object to facilitate unit
testing of code talking to a back-end.


With all the data loaded on the client side, and available in JavaScript we are ready
to dive into various ways of rendering those data with AngularJS. The next chapter is
fully devoted to templates, directives and rendering.

Free download pdf