代码之家  ›  专栏  ›  技术社区  ›  mofury

ActionCable订阅创建方法的可用回调列表是什么?

  •  4
  • mofury  · 技术社区  · 7 年前

    我正在尝试设置Rails 5 ActionCable 用于向我的数据库广播更新。到目前为止,我的工作进展顺利,但我意识到ActionCable上的文档有点缺乏。对于我的例子,我想知道允许放入函数中的回调列表 subscriptions.create() .

    const consumer = ActionCable.createConsumer();
    consumer.subscriptions.create(
        'ChatsChannel'
        {
            received: someCallback,
            connected: otherCallback,
            disconnected: anotherCallback
        }
     )
    

    我注意到有 appendLine createLine

    第5.4节 http://guides.rubyonrails.org/action_cable_overview.html

    open , close , error message ActionCable 什么时候Rails应该是常规配置?

    谢谢

    2 回复  |  直到 7 年前
        1
  •  2
  •   ChillyFlashER coconup    6 年前

    从源代码来看,这是仅有的三个现成的回调:

    connected

    received

    disconnected

        2
  •  0
  •   Juan José Ramírez    6 年前

    在您指向的部分中 guide :

    # app/assets/javascripts/cable/subscriptions/chat.coffee
    App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
      received: (data) ->
        @appendLine(data)
    
      appendLine: (data) ->
        html = @createLine(data)
        $("[data-chat-room='Best Room']").append(html)
    
      createLine: (data) ->
        """
        <article class="chat-line">
          <span class="speaker">#{data["sent_by"]}</span>
          <span class="body">#{data["body"]}</span>
        </article>
        """
    

    appendLine createLine 这里定义了回调,由 @appendLine @createLine 分别地这只是CoffeeScript语法,可以更直观地对代码进行模块化。

    还有多少?

    老实说,我不确定。我还没有找到任何JS文档像 the ruby documentation 附录 创建线 不是定义的回调,它们仅出现在该示例中。

    可以找到完整的列表 here