AngularJS Essentials
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Dependency Injection and Services<br />
};<br />
});<br />
retrieveCars();<br />
});<br />
Creating an HTTP facade<br />
Now, we have the opportunity to evolve our design by introducing a service that<br />
will act as a facade and interact directly with the backend. The mapping of each<br />
URL pattern should not be under the controller's responsibility; otherwise, it could<br />
generate a huge amount of duplicated code and a high cost of maintenance.<br />
In order to increase the cohesion of our controller, we moved the code responsible to<br />
make the calls to the backend of the parkingHttpFacade service, as follows:<br />
services.js<br />
parking.factory("parkingHttpFacade", function ($http) {<br />
var _getCars = function () {<br />
return $http.get("/cars");<br />
};<br />
var _getCar = function (id) {<br />
return $http.get("/cars/" + id);<br />
};<br />
var _saveCar = function (car) {<br />
return $http.post("/cars", car);<br />
};<br />
var _updateCar = function (car) {<br />
return $http.put("/cars" + car.id, car);<br />
};<br />
var _deleteCar = function (id) {<br />
return $http.delete("/cars/" + id);<br />
};<br />
return {<br />
getCars: _getCars,<br />
getCar: _getCar,<br />
saveCar: _saveCar,<br />
[ 82 ]<br />
www.it-ebooks.info