【发布时间】:2017-05-07 22:05:32
【问题描述】:
我的 application.html.haml 中有一个代码,它根据某些用户属性决定是否为用户订阅给定频道。
问题是,鉴于我在正文中有这段代码,当我单击要重定向到另一个页面的链接时,它会再次订阅用户而不是保持旧连接,所以我有 receive()方法被多次执行。
这是我在application.html.haml中的代码:
%html
%head
-# other stuff
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
-# other stuff
- if current_user.is_admin?
:coffee
MyApp.AdminNotifications.init()
assets/javascripts/notifications/admin_notifications.js.coffee
window.MyApp.AdminNotifications = class AdminNotifications
@init: ->
App.cable.subscriptions.create "ProductsChannel",
received: (data) ->
console.log 'Data Received' # This is being executed multiple times
第一次加载页面时,当我向该频道广播消息时,控制台只记录一次“收到的数据”。 我单击要重定向到另一个页面的链接,我向该频道广播了一条新消息,现在控制台正在记录“接收到的数据”两次。到目前为止……
其实,当我在 chrome 的控制台上运行时:
App.cable.subscriptions.subscriptions
它返回对同一频道的多个订阅(每次我点击一个链接并被重定向到不同的页面时一次)。
注意:我没有在这篇文章中添加更多动作电缆设置,因为它工作正常。
我怎样才能避免这种情况?有什么想法吗?
【问题讨论】:
标签: javascript ruby-on-rails turbolinks actioncable turbolinks-5