Reputation: 1365
I have a website with a little bell, and when a new notification comes, the @notifications.lenght is passed via javascript for showing the number of notifications. What i just want is to show the number of notification if there is one or more notifications and if there is no notification showing just the bell like this :
Actually if the user has no notification the page displays this :
If i click the bell :
Witch is good, but when i refresh the page :
Here is my coffee :
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
@setup() if @notifications.length > 0
setup: ->
$("[data-behavior='notifications-link']").on "click", @handleClick
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleClick: (e) =>
$.ajax(
url: "/notifications/mark_as_read"
dataType: "JSON"
method: "POST"
success: ->
$("[data-behavior='unread-count']").text("")
)
handleSuccess: (data) =>
items = $.map data, (notification) ->
"<a class='dropdown-item' href='#{notification.url}'>#{notification.actor} #{notification.action} </a>"
$("[data-behavior='unread-count']").text(items.length)
$("[data-behavior='notification-items']").html(items)
jQuery ->
new Notifications
How could i pass an empty string if the @notifications.lenght == 0 ?
Upvotes: 0
Views: 239
Reputation: 2295
In your handle success method, replace the code that set the total items to:
$("[data-behavior='unread-count']").text(parseInt(items.length) || "");
If items.length
is 0
, it will set an empty string.
Upvotes: 1