jquery - 在“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 存储库链接。我认为我的问题与“数据”返回一个对象而不是一个数组有关。
解决方案
我确定这是一个错字,因为它map
是一个函数,而数据是这个函数的一个参数。因此,以下应该是该部分脚本的正确版本:
handleSuccess: (data) =>
items = $.map data, (notification) ->
推荐阅读
- php - 在包含的字符串中搜索 URL
- google-bigquery - 如何在 bigquery 中使用 String_agg
- google-cloud-platform - Dataproc 上的抢占式工作虚拟机上的 GPU 支持
- authentication - TYPO3 v10.2:自定义身份验证服务中的前端用户会话保存 ($GLOBALS['TSFE']->fe_user)
- delphi - Word Automation 的 SaveAs 不会将文件保存在一台机器的特定文件夹中
- fonts - 如何使 Arch Linux 上的 Snap Store 字体显示为文本而不是空矩形?
- java - latlng 不能为空 - 自动完成中需要一个位置
- python - pycharm-package/main/main 在 tmp 中不存在,我该如何解决?
- ios - 无法使用 Alamofire 上传图片
- python - 按条件删除行