Created
December 11, 2012 07:06
-
-
Save jrmoran/4256498 to your computer and use it in GitHub Desktop.
AngularJS intercept responses - array of strings to array of objects
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
app.config( function( $httpProvider ) { | |
var interceptor = function( $q ) { | |
return function( promise ) { | |
var success = function(res) { | |
if(res.data && res.data[0]){ | |
if(typeof res.data[0] === 'string'){ | |
var data = res.data, | |
ret = []; | |
angular.forEach(data, function(d){ | |
ret.push({data: d}); | |
}); | |
res.data = ret; | |
return res; | |
} | |
} | |
}; | |
var error = function(res) { }; | |
promise.then( success, error ); | |
return promise; | |
} | |
}; | |
$httpProvider.responseInterceptors.push( interceptor ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment