Skip to content

Instantly share code, notes, and snippets.

@prettycode
Last active August 29, 2015 14:10
Show Gist options
  • Save prettycode/0cf9e1f4d3d0a777704c to your computer and use it in GitHub Desktop.
Save prettycode/0cf9e1f4d3d0a777704c to your computer and use it in GitHub Desktop.
AngularJS Data Service pattern
// a la http://chariotsolutions.com/blog/post/angularjs-corner-using-promises-q-handle-asynchronous-calls/
// http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/
angular
.module('movie')
.factory('movieService', function ($http, $log, $q) {
return {
getMovie: function (movie) {
var deferred = $q.defer();
$http.get('/api/v1/movies/' + movie)
.success(function (data) {
deferred.resolve({
title: data.title,
cost: data.price
});
}).error(function (msg, code) {
deferred.reject(msg);
$log.error(msg, code);
});
return deferred.promise;
}
}
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment