【发布时间】:2016-01-12 03:02:34
【问题描述】:
我有一个带有小铃铛的网站,当有新通知到来时,@notifications.lenght 通过 javascript 传递以显示通知的数量。 我只想在有一个或多个通知时显示通知的数量,如果没有通知只显示这样的铃铛:
实际上,如果用户没有收到通知,页面会显示:
如果我点击铃铛:
女巫很好,但是当我刷新页面时:
这是我的咖啡:
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
如果@notifications.lenght == 0,我怎么能传递一个空字符串?
【问题讨论】:
标签: javascript ruby-on-rails coffeescript notifications