Last active
August 29, 2015 13:56
-
-
Save quantizor/8851302 to your computer and use it in GitHub Desktop.
Aspected DFD - get before & after functionality for your Angular $http requests
This file contains 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
/* | |
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