[英]Coffee script conditional rails
我有一个带有小铃铛的网站,当收到新通知时,会通过JavaScript传递@ notifications.lenght,以显示通知数量。 我只想要显示通知号,如果有一个或多个通知,并且没有通知,只显示这样的铃声:
实际上,如果用户没有通知,页面将显示以下内容:
如果我按一下钟声:
女巫很好,但是当我刷新页面时:
这是我的咖啡:
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,如何传递空字符串?
在您的成功处理方法中,替换将总项设置为的代码:
$("[data-behavior='unread-count']").text(parseInt(items.length) || "");
如果items.length
为0
,它将设置一个空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.