Skip to content

Instantly share code, notes, and snippets.

@mdonkers
Created January 5, 2014 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdonkers/8270041 to your computer and use it in GitHub Desktop.
Save mdonkers/8270041 to your computer and use it in GitHub Desktop.
Extending one AngularJS service by another service, modifying input parameters and response.
myApp.factory('couchService', function (basicCouchService) {
return {
get: function(listType, response) {
// Convert the input parameters to the schema-names used inside CouchDB
var couchSchema = (listType.listType === 'latest') ? 'by_submit' : 'by_duration';
basicCouchService.get({listType: couchSchema}, function (localResponse) {
// Get the top-10, assume the array is already ordered wrong way around.
var rowList = localResponse.rows;
rowList = rowList.slice(rowList.length - 10, rowList.length);
localResponse.rows = rowList;
response(localResponse);
});
}
};
});
myApp.factory('basicCouchService', function ($resource, $http, Base64) {
return $resource('/mydb/_view/:listType',{
listType: '@listType'
}, {
get: {
method: 'GET',
isArray: false,
headers: {'Authorization': 'Basic ' +
Base64.encode('username' + ':' + 'password')}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment