Lets say i have several $resources and some $http around my angular application:
myApp.factory('Note', function($resource) {
return $resource('http://', {id: '@id'},
{ 'index': { method: 'GET', isArray: true },
'update': { method: 'PUT'},
});
});
with controller
myApp.controller('NotesController',function NotesController($scope, Note, AuthenticationService) {
$scope.notes = Note.index({}, function(data){
console.log('success, got data: ', data);
$scope.response = "yoy!"
}, function(err){
console.log('error, got data: ', err);
$scope.response = "yay!"
});
});
and some request are made by $http directly like authentication
var request = $http.post('http://', {email: email, password: password});
Where and How i can tell angular to deflate and encode/decode JSONs to base64 before the actual request is made / response is receive?
I quess i will wrap external libraries for deflate and encode/decode into factory. And then this factory will be injected somehere? Like $httpBackend ?
You should have a look at the request / response transformers for the
$http
service: http://docs.angularjs.org/api/ng.$httpRequest / response transformers are simply functions that can be invoked before content is sent / handed back to the caller. You can specify transforming functions globally (for all requests / responses) as well as per-request basis:
To define global request / response transformers one would write the code along those lines (it is more like pseudo-code, won't work in all browsers, see notes about Base64 below):
Of course your transforming code could be more sophisticated and depend on request / response headers but the general idea is here. The jsFiddle with the code (check the console to see that a request gets transformed, you need to use Mozilla or a WebKit browser): http://jsfiddle.net/Ydt5j/
For the actual conversion from / to Base64 check this question: How can you encode a string to Base64 in JavaScript?
this is how i do in filter. item.htmlBody contains base64 encoded text with html tag
well, i know this doesnt exactly answer the question, but with some minor tweak, you can get what you want (targetting future person with the same problem)