事件处理程序未触发Ajax请求

时间:2018-06-26 23:04:37

标签: javascript ruby-on-rails ajax coffeescript

我有这个脚本:

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("")               

$(document).on('turbolinks:load', -> new Notifications)

除了用于.on“ click”事件调用(它不会触发@handleClick函数)之外,它还可以正常工作。有什么想法吗?

0 个答案:

没有答案
相关问题