-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (55 loc) · 1.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
console.log('app running')
// ES5 JQuery version
// var allPokemon = []
//
// $.ajax({
// method: 'GET',
// url: 'http://pokeapi.co/api/v2/pokemon/',
// success: function (res) {
// console.log('successfully communicated with api', res)
// allPokemon = res.results
// var p = new Pokemon()
// for (var i = 0; i < allPokemon.length; i++) {
// p.createPokemon(allPokemon[i])
// }
// },
// error: function (err) {
// console.log('something went wrong with the api', err)
// }
//
// })
// obj.myfunc.anotherfunc
// ES6 FETCH
fetch('http://pokeapi.co/api/v2/pokemon/')
.then(res => res.json()) // turning a json string js object
.then(pokemon => {
let myPoke = new Pokemon()
pokemon.results.map(p => myPoke.createPokemon(p))
})
class Pokemon {
constructor () {
this.pokeDiv = document.querySelector('.pokemon')
this.dataDiv = document.getElementById('pokedata')
this.pokeDiv.addEventListener('click', this.displayPokeData.bind(this))
}
createPokemon (pokemonObj) {
this.pokeDiv.innerHTML += `<p id="${pokemonObj.name}">${pokemonObj.name}</p>`
}
displayPokeData () {
let pokemonName = event.target.id
this.getSinglePokemon(pokemonName)
}
getSinglePokemon (name) {
fetch(`http://pokeapi.co/api/v2/pokemon/${name}`)
.then(res => res.json()) // method chaining
.then(this.createPokemonData.bind(this))
}
createPokemonData (pokemon) {
this.dataDiv.innerHTML = `<div>
<img src="${pokemon.sprites.front_shiny}"/>
<p>${pokemon.name}</p>
<p>${pokemon.height}</p>
<p>${pokemon.weight}</p>
</div>`
}
}