Book Image

AngularJS Web application development Cookbook

By : Matthew Frisbie
Book Image

AngularJS Web application development Cookbook

By: Matthew Frisbie

Overview of this book

Packed with easy-to-follow recipes, this practical guide will show you how to unleash the full might of the AngularJS framework. Skip straight to practical solutions and quick, functional answers to your problems without hand-holding or slogging through the basics. Avoid antipatterns and pitfalls, and squeeze the maximum amount out of the most powerful parts of the framework, from creating promise-driven applications to building an extensible event bus. Throughout, take advantage of a clear problem-solving approach that offers code samples and explanations of components you should be using in your production applications.
Table of Contents (17 chapters)
AngularJS Web Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using promises with $resource


As part of the ngResource module, $resource provides a service to manage connections with RESTful resources. As far as vanilla AngularJS goes, this is in some ways the closest you'll get to a formal data object model infrastructure. The $resource tool is highly extensible and is an excellent standalone tool upon which to build applications if third-party libraries like Restangular aren't your cup of tea.

As the API-focused wrapper for $http, $resource also provides an interface for using promises in conjunction with the HTTP requests that it generates.

How to do it…

Although it wraps $http, $resource actually does not use promises in its default implementation. The $promise property can be used to access the promise object of the HTTP request, as follows:

// creates the resource object, which exposes get(), post(), etc.
var Widget = $resource('/widgets/:widgetId', {widgetId: '@id'});

// resource object must be coaxed into returning its promise
// this can be done...