【发布时间】:2018-06-17 16:14:53
【问题描述】:
我最近在我的 Rails 应用程序中构建了一个通知功能,来自 GoRails => Here's the tut
该方法的长短是创建一个通知模型,该模型记录涉及某些操作的用户之间的关联(即,发布帖子将创建一个通知 b/t 发布者和发布内容的所有者) .
通知还具有一个名为“读取”的属性,默认情况下该属性为假。问题从这里开始。虽然通知正确保存,但只要我以假设接收通知的用户身份登录,就会向我的服务器发送一个 POST 请求,将“读取”更改为 true。下面是负责发出请求的脚本和视图。
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) =>
console.log(data)
items = $.map data, (notification) ->
"<a class='dropdown-item' href='#{notification.url}'>#{notification.actor} #{notification.action} #{notification.notifiable.type}</a>"
console.log(items)
$("[data-behavior='notification-items']").html(items)
$("[data-behavior='unread-count']").text(items.length)
if items.length is 0
$("[data-behavior='unread-count']").text("")
jQuery ->
new Notifications
还有观点:
<li class="nav-item dropdown" data-behavior='notifications' data-behavior="notifications-link">
<a id="notificationsDD" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">
<%= fa_icon("bell") %><span data-behavior='unread-count'></span>
</a>
<div class="dropdown-menu" data-behavior="notification-items" aria-labelledby="notificationsDD">
<!--
<a class='dropdown-item' href='#'>yeo</a>
-->
</div>
</li>
从修改脚本来看,@handleClick 函数似乎是在自行运行而没有发生点击事件。
【问题讨论】:
-
您能否修复 CoffeeScript 中的缩进以匹配它的真实外观?缩进定义了 CoffeeScript 代码的结构,因此正确使用它很重要。
-
我认为它的格式正确。作为参考,这只是我链接到的 tut 中的代码,如果你向下滚动页面,向底部但在 cmets 之前,你会看到我在说什么
-
查看this version 和this (correct) version 的JavaScript,你就会明白我对缩进的意思。我很确定问题仅在于问题的格式。
标签: javascript ruby-on-rails coffeescript