Last active
November 30, 2018 23:23
-
-
Save chamatt/bf29d00cf4cbe78a8b343cecbebbf6f2 to your computer and use it in GitHub Desktop.
Obter Posts
This file contains hidden or 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
| function ObterPosts() { | |
| const secaoblog = document.querySelector("section.blog-container"); // Seleciona o elemento do DOM | |
| axios.get("http://localhost:3000/posts").then(resposta => { | |
| // Faz uma Array com os posts no formato do template | |
| const ListaDePosts = resposta.data.map(post => PostTemplate(post.title, post.body, post.image, post.id)); | |
| let PostsJuntos = ListaDePosts.reverse().join(); // Inverte a lista e junta todos os posts em uma string só | |
| if (ListaDePosts.length == 0) // Checamos se existe pelo menos um post no banco de dados, senão, mostramos uma mensagem. | |
| PostsJuntos = `<div class="alert alert-info" role="alert"> Nenhum post! Crie um novo! </div>`; | |
| secaoblog.innerHTML = PostsJuntos; // Insere a string dentro da seção dos posts | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment