Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10 lesson #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

10 lesson #9

wants to merge 10 commits into from

Conversation

rusrich
Copy link
Owner

@rusrich rusrich commented Jul 14, 2020

No description provided.

const gifs = document.querySelector('#gifs')
if (gif_result) {
for (let i =0; i < gif_result.data.length; i++) {
const gif = document.createElement('div')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

эту логику отвечающую за отрисовку стоило бы вынести в отдельную функцию

// getFunc(event.target.value)
// }, 500))

// Не знаю как избавить от глобальной переменной gif_result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вызывать createElements сразу в том месте, где мы получаем эти данные, передавая их в качестве аргумента

// При первом вводе получаю данные, видно в консоли
// Если делаю повторный запрос с другими данными, то получаю новые данные,
// Но элементы картинки отображаются с первого запроса
const debounce = (fn, delay) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут есть важный нюанс, debounce не используется напрямую, нам нужно сначала обернуть им функцию и потом использовать ее:
const debouncedGet = debounce(event => { getFunc(event.target.value) }, 500)

и вот теперь используем debouncedGet

const getFunc = async (query) => {
const result = await getGif(query)
const result2 = await createEl()
const result_cache = await cacheImages()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сейчас cacheImages работает сложно, по сути внутри нее мы модифицируем 2 разных глобальные переменные, за изменениями очень трудно следить, лучше заменить это на простую логику внутри getFunc

if (responses[query) { // возвращаем результат из кэша и отрисовываем его } else { // запрашиваем результат с сервера // ... // не забываем записать его в кэш responses[query] = result }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants