问题描述
我正在尝试向Rails应用程序添加通知。基本上,我是从JSON端点获取它们的,然后尝试使用某些Coffeescript 。问题出在handleSuccess方法中,我遇到了未引用的错误,通知未定义
I am trying to add notifications to my rails app. Basically I get them from a JSON endpoint and I try to fetch them and append them to my html using some CoffeescriptHere's the JSON array. The problem is in the handleSuccess method where I get uncaught reference error, notification not defined
notifications.js.coffee
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) =>
console.log(data)
items = $.map data, (notification) ->
"<a class='dropdown-item' href='#{notification.url}'>#
{notification.action} #{notification.notifiable.type}</a>"
jQuery ->
new Notifications
我从localhost获得的信息:3000 / notifications.json
[{ actor: emanuelcoen, action: liked, notifiable:{ type:您的列表} , url: / lists / 10#list_10},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url : / lists / 10#list_10},{ actor: emanuelcoen, action: liked, notifiable:{ type:您的列表}, url: / lists / 10#list_10},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 10#list_10 },{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action: 喜欢,通知 ble:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢, notifiable:{ type: 您的列表}, url: / lists / 17#list_17},{ actor: emanuelcoen, action:喜欢,可通知:{ type:您的列表 }, url: / lists / 17#list_17}]
[{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"}]
推荐答案
该函数中存在缩进错误正在传递到您的 $。map
。它下面的字符串必须缩进,否则它假定您正在传递一个要映射的空函数,并且由于未定义 notification
,因此它后面的行会引发错误。 / p>
You have an indentation error in the function you are passing to your $.map
. The string below it has to be indented, otherwise it assumes you are passing an empty function to map, and the line after it raises an error as notification
isn't defined.
handleSuccess: (data) =>
console.log(data)
items = $.map data, (notification) ->
"<a class='dropdown-item' href=''>#{notification.actor}</a>"
更新
关于通知未显示在页面上的注释-您未调用任何代码将要生成的html字符串添加到DOM。您可以为此使用。
handleSuccess: (data) =>
console.log(data)
for notification in data
@notifications.append(
"<a class='dropdown-item' href=''>#{notification.actor}</a>")
无需使用 $。map
放在通知数组上,因为我们只是在另一个循环中渲染它们,所以我用单个Coffeescript理解替换了它。
There is no need to use $.map
over the notifications array as we are just rendering them in another loop, so I replaced it with a single Coffeescript comprehension.
这篇关于未捕获的参考错误,未定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!