All threads / How do I include a placeholder for empty notifications

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I include a placeholder for empty notifications

Stan Smith asked in General

Your In-App Notification code worked perfectly.
However I'm stuck on what code to use to display a placeholder message if there aren't any notifications.

You mentioned this briefly in the episode but didn't offer an example.

Hey Stan,

You'd actually just put an if statement in the JS. If the length of the notifications > 0, display them, else, render your placeholder.

Something like this (I haven't tested this, so it may have some bugs):

  handleSuccess: (data) =>
    items = $.map data, (notification) ->
      "<a class='dropdown-item' href='#{notification.url}'>#{notification.actor} #{notification.action} #{notification.notifiable.type}</a>"

    $("[data-behavior='unread-count']").text(items.length)

    if items.length == 0
      items = "<a>No new notifications</a>"

    $("[data-behavior='notification-items']").html(items)

That did it. Thanks Chris!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.