Last active
August 29, 2015 14:10
-
-
Save prettycode/0cf9e1f4d3d0a777704c to your computer and use it in GitHub Desktop.
AngularJS Data Service pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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