我现在正试图摆脱 Turbolinks,它让我的 JS 有点混乱。
虽然它有点破坏了我的聊天室咖啡脚本代码。
(我对JS知之甚少,咖啡更甚)
这是我的咖啡:
App.conversationsdevis = App.cable.subscriptions.create "ConversationsdevisChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
$('#cellmessagesde'+data.cellid).append '<div class="'+data.auteur+'">'+'<span>'+data.message+'</span>'+'</div>'
$(document).on 'turbolinks:load', ->
submit_message()
submit_message = () ->
$('.sendmessageinputtext').on 'keydown', (event) ->
if event.keyCode is 13
$('input').click()
event.target.value = ""
event.preventDefault()
我认为聊天消息只是因为这一点而被注入$(document).on 'turbolinks:load'
有没有办法注入另一种咖啡条件而不是 Turbolinks 负载?
'turbolinks:load'
是一个 turbolinks 事件,当文档在初始页面加载时准备好或 turbolinks 完成替换文档内容时由 turbolinks 触发。
相反,您想要的是使用jQueryready
函数来保证文档已完全解析并准备好进行操作。
$.ready ->
submit_message()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句