Reputation: 3080
I have an AngularJS Factory (holds collection of Objects), and I am trying to find what item a user is requesting.
Factory
angular.module('app').factory('APIMethodService', [function() {
return {
apis: [
{
accounts: [
{
parameters : [
{
name : "Account",
version : "1.0"
}
],
uri: Head + "/v1/accounts/account_number",
methods : [
{
name: "Account Number",
desc: "Returns the account number."
}, {
name: "Account Money",
desc: "Returns the monetary amount within the account."
}
]
},
{
parameters : [
{
name : "Account",
version : "2.0"
}
],
uri: Head + "/v2/accounts/account_number",
methods: [
{
name: "Account Number",
desc: "Returns the account number."
}, {
name: "Account Money",
desc: "Returns the monetary amount within the account."
}, {
name: "Account Token",
desc: "Returns the account's token."
}
]
}
],
customers:[
{
parameters : [
{
name : "Customers",
version : "1.0"
}
],
uri: Head + "/v1/customers/customer_number",
methods : [
{
name: "Customer Name",
desc: "Returns the customer's name."
}, {
name: "Customer ID",
desc: "Returns the customer's ID."
}
]
},
{
parameters : [
{
name : "Customers",
version : "2.0"
}
],
uri : Head + "/v2/customers/customer_number",
methods: [
{
name: "Customer Name",
desc: "Returns the customer's name."
}, {
name: "Customer ID",
desc: "Returns the customer's ID."
}, {
name: "Customer Email",
desc: "Returns the customer's email."
}
]
}
]
}
]
};
}]);
Controller
angular.module('app').controller('apiTabController', ['$scope', '$route', '$location', 'APIMethodService', function($scope, $route, $location, APIMethodService) {
$scope.params = $route.current.params;
if ($scope.data == null && Object.keys($scope.params).length > 0) {
console.log("came from URL");
// Variables I get from URL
var name = $scope.params.name;
var version = $scope.params.version;
// My list of API's
$scope.currentAPI = APIMethodService.apis;
for (var i = 0, len = $scope.currentAPI.length; i < len; i++) {
// FIND THE API OBJECT I NEED
}
}
}]);
So I pull in name and version from the URL parameters (like apiTab?name=Customers&version=1.0
), and then I am trying to get the right object from my Factory that matches the parameters.
So If I wanted Customers, version 1.0, that would be $scope.currentAPI[0].customers[0]
, and the URL that would have given me this would be apiTab?name=Customers&version=1.0
But I am unsure how to search and match up the right factory object..
Upvotes: 0
Views: 55
Reputation: 8325
You can write logic inside service to get proper object, or other possible solution could be to replace for loop in your controller with following one, it handles best case only:
var matchAPIObject = {}; // to capture right object
for(var i in currentAPI){
var targetAPI = currentAPI[i][name.toLowerCase()];
for(var j in targetAPI){
//console.log(targetAPI[j].parameters[0].name.toLowerCase());
if(targetAPI[j].parameters[0].name.toLowerCase() === name.toLowerCase()
&& targetAPI[j].parameters[0].version === version ){
matchAPIObject = targetAPI[j]; //found it now get me out
break;
}
}
}
Happy Helping!
Upvotes: 1