Skip to content

Instantly share code, notes, and snippets.

@quantizor
Last active August 29, 2015 13:56
Show Gist options
  • Save quantizor/8851302 to your computer and use it in GitHub Desktop.
Save quantizor/8851302 to your computer and use it in GitHub Desktop.
Aspected DFD - get before & after functionality for your Angular $http requests
/*
This function allows you to define things happen before and after the receiving
controller actually gets its data.
Useful for caching the result set locally in a service.
After comes prior to before because we want don't want to delay return of data to the
controller unless absolutely necessary.
Requires access to $q.
Use it like this:
return aspectedDFD(
$http.get('/yourAPIEndpoint'),
function(){
// do something with the data after
},
function(){
// do something with the data before
},
);
*/
function aspectedDFD( realDFD, after, before ){
if( realDFD ){
var dfd = $q.defer();
realDFD.then(function(){
if ( before ) before.apply(this, arguments);
dfd.resolve.apply(this, arguments);
if ( after ) after.apply(this, arguments);
});
return dfd.promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment