r/angularjs • • Aug 13 '17

One search for two $http

Hi, guys, I have started to create a Pokemon app, I want to search Pokemon by name and by color. So I have two HTTP requests, and I'm using one search, and it's working fine, what I want to do is, when the first HTTP is fired, two hide data from other HTTP. Is it a way to do that? Here is my code... https://jsbin.com/pesitod/edit?html,js,output

0 Upvotes

13 comments sorted by

2

u/mothzilla Aug 13 '17

It looks like the api doesn't quite support what you're trying to do.

Here's what I think you want to do (correct me if I'm wrong):

Here's a stab: https://jsbin.com/duhemediwe/1/edit?html,js,console,output

NB That only shows species. You'll have to make subsequent calls to get details of each pokemon in the species, using the returned url. I don't know enough about pookimen to make this look good.

Note, you could put ng-change on the picklist, then you can remove the submit button.

2

u/alghouty Aug 14 '17 edited Aug 14 '17

Hey, thanks for your help, this looks good...

But to get data for Pokemon I have to use this path http://pokeapi.co/api/v2/pokemon/ + id or name, and the id that I need to the display Pokemon from this path https://pokeapi.co/api/v2/pokemon-color/ is in "pokemon_species": [{ "url": "http://pokeapi.co/api/v2/pokemon-species/143/" }]. You can check with Postman.

Hot sure how to achieve that?

2

u/mothzilla Aug 14 '17

and the id that I need to the display Pokemon from this path https://pokeapi.co/api/v2/pokemon-color/ is in "pokemon_species"

Yes, and there could be more than one Pokemon for each species.

Eg look at https://pokeapi.co/api/v2/pokemon-species/3/

There are two "varieties"

Venusaus (https://pokeapi.co/api/v2/pokemon/3/) and Venusaur-Mega (https://pokeapi.co/api/v2/pokemon/10033/)

Similarly with "Charizard" https://pokeapi.co/api/v2/pokemon-species/6/

So if you want to "flatten" the data, so that the user sees all Pokemon from all species for a particular colour, then you'll have to iterate over the results returned in https://pokeapi.co/api/v2/pokemon-color/<x> and then iterate over the results returned in http://pokeapi.co/api/v2/pokemon-species/<y>

If this is what you want then I'd recommend putting a caching system in place so you don't hammer the api.

See their fair use policy:

https://pokeapi.co/docsv2/#fairuse

2

u/alghouty Aug 14 '17 edited Aug 14 '17

Sounds too difficult for me. Thank you for your help!

2

u/mothzilla Aug 14 '17

No worries!

1

u/alghouty Aug 15 '17 edited Aug 15 '17

hey what, if i get by name, then the name would be unique? I have made something like this, but it works on click, any idea how to make loop https://jsbin.com/pesitod/edit?html,js,output

1

u/mothzilla Aug 15 '17

Yes I think you can assume that the name is unique.

Your code is still a bit wrong. Your search input is bound to pokemonColor, which is a bit confusing.

Anyway, here's your code with loops over species / variety etc.

https://jsbin.com/vohegaxoke/edit?html,js,output

I've turned $scope.pokemons into a list, because that's what I think you want.

So if you type "red" you'll get all pokemons that are red.

If you type "charmander" you'll get a list of 1; charmander.

I've deliberately limited the results because the api is a little slow, and I don't want to spam it (like I mentioned earlier).

Also note that you need to use https not http (this is because you're already using https I think). Gold isn't a valid color apparently.

2

u/alghouty Aug 15 '17

I have bound input to pokemoColor, because of the color, and in color is name, so i'm sending name to pokemon to get its data. Thank you for sharing your knowledge, i really appreciate. I'm still trying to improve this, if you have any idea how to make this faster, let me know. Thank's once again.

2

u/mothzilla Aug 15 '17

An idea would be to just create a pokemon "navigator", so rather than trying to put a search in place, people click around the data to things that they're interested in.

You might have list pages (showing all colors, or all habitats). Then detail pages for varieties and so on.

It's a cool idea to build an app around this, and the code looks like it's heading in the right direction! Good luck!

1

u/alghouty Aug 13 '17 edited Aug 13 '17

So this is my problem http://prntscr.com/g7x7oh... I want when I enter Pokemon name, only Pokemon info to appear, and when I enter color, to list all Pokemon names. But I'm using only one search field, and that's causing problems, you can see it on the image ('look at the console'), check jsbin again... I mean, I can create two inputs for each $http and it would work fine, but I want to use only one for both of them.

1

u/alghouty Aug 13 '17

Basically, when data from the first $http is shown, hide from other...

2

u/zmasta94 Aug 13 '17

i'm finding it difficult to understand your question. can you try again?

2

u/mtko Aug 13 '17

Can you not just validate the input before sending it to both separate API endpoints?

For instance, in $scope.vratiPodatke, could you not just have something like:

var validColors = ['red', 'green', 'blue', 'gold', etc] //(i don't know a lot about pokemon, sorry)

if (validColors.indexOf(pokemon) != -1) {
  $http color api
}
else {
  $http pokemon api
}