首页 > 解决方案 > 在“GoRails”之后在我的 Rails 应用程序中实现通知

问题描述

我一直在关注这个关于通知系统的教程,在我开始编写 Coffeescript 之前,一切都很顺利。这个有问题的脚本是:

class Notifications
constructor: ->
    @notifications = $("[data-behavior='notifications']")
    @setup() if @notifications.length > 0 

setup: -> 
    $.ajax(
        url: "/notifications.json"
        dataType: "JSON"
        method: "GET"
        success: @handleSuccess
    )

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

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

jQuery ->
  new Notifications

我得到的错误来自 $.map.data 后面的“,”。当我删除它时,我收到一个新错误,这次是在控制台中说:

Uncaught TypeError: $.map.data is not a function

教程的链接

它包括一个视频、带代码的脚本和一个 GitHub 存储库链接。我认为我的问题与“数据”返回一个对象而不是一个数组有关。

标签: jqueryruby-on-railsajaxcoffeescript

解决方案


我确定这是一个错字,因为它map是一个函数,而数据是这个函数的一个参数。因此,以下应该是该部分脚本的正确版本:

handleSuccess: (data) =>
   items = $.map data, (notification) ->

推荐阅读