Created
January 5, 2014 16:08
-
-
Save mdonkers/8270041 to your computer and use it in GitHub Desktop.
Extending one AngularJS service by another service, modifying input parameters and response.
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
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