【问题标题】:Coffee script conditional rails咖啡脚本条件栏
【发布时间】: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


    【解决方案1】:

    在你的处理成功方法中,将设置总数的代码替换为:

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

    如果items.length0,它将设置一个空字符串。

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 2011-06-30
      • 2013-06-10
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多