Skip to content
New issue

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

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

Already on GitHub? # to your account

Example with each loop #63

Open
mattpilott opened this issue Oct 27, 2022 · 1 comment
Open

Example with each loop #63

mattpilott opened this issue Oct 27, 2022 · 1 comment

Comments

@mattpilott
Copy link

Would be handy to see how this works in an each loop in the examples

@infinito84
Copy link

@mattpilott I did something like this

<script>
  import { onMount } from "svelte"
  import IntersectionObserver from 'svelte-intersection-observer'

  let notifications = []
  let notificationsDom = []

  onMount(() => {
    setInterval(loadNotifications, 30000);
    loadNotifications()  
  })

  async function loadNotifications() {
    const response = await fetch('/api/notifications')
    notifications = (await response.json())
    notificationsDom = new Array(notifications.length)
  }

  async function markReadNotificationId(id) {
    console.log(id)
  }
</script>
<div class="list-group list-group-flush list-group-activity my-n3">
  {#each notifications as notification, i}
    <IntersectionObserver element={notificationsDom[i]} on:intersect={() => {
      markReadNotificationId(notification.id)
    }}>
      <a class="list-group-item text-reset" bind:this={notificationsDom[i]} href="{notification.link}">
        ...
      </a>
    </IntersectionObserver>
  {/each}
</div>

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

No branches or pull requests

2 participants