1

Curious what the best practice is for using ActionCable with Turbolinks, when you want to tie a channel with the page being viewed.

The classic example, of course, if you had an article with comments -- how can you stream only those comments related to the article in view and then subscribe to a different channel when viewing a different article?

I've played around with using turbolinks:loaded event on JQuery, but cant figure out what to link that to. Do I want to resubcribe every time? How is that possible without reloading the JS?

4

2 回答 2

5

我通过这样做来做到这一点:

(直播任务输出)

$(document).on 'turbolinks:load', -> # use page:change if your on turbolinks < 5
  if document.getElementById("task-output") # if a specific field is found 
    App.task = App.cable.subscriptions.create { channel: "TaskChannel", task_id: task_id },
      received: (data) ->
        # do something with your data
  else if App.task # if I'm not on the page where I want to be connected, unsubscribe and set it to null
    App.task.unsubscribe()
    App.task = null
于 2016-04-10T11:44:52.910 回答
0

另一种方法是

  1. 将您的频道 js 移动到单独的目录。
  2. 将您的特定频道 js 添加到 rails 资产管道。
  3. 编辑您的应用程序布局并添加内容占位符。
  4. 在您的特定页面上,为上述占位符提供内容并包含您的特定频道 js。

在我们的博客上,您将找到有关如何在特定页面上启用动作电缆的更详细的帖子。

于 2017-08-19T13:54:27.957 回答