Rails的actionCable5

时间:2018-07-16 11:15:47

标签: ruby-on-rails actioncable

在此tutorial

我几乎可以理解。

但是只有一部分我不能。

//javascripts/channels/rooms.coffee

App.global_chat = App.cable.subscriptions.create {
    channel: "ChatRoomsChannel"
    chat_room_id: ''
  },
  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) ->
    # Data received

  send_message: (message, chat_room_id) ->
    @perform 'send_message', message: message, chat_room_id: chat_room_id

具有内容send_message的方法@perform 'send_message', message: message, chat_room_id: chat_room_id。将其显示为Javascript:

function(message){
 return this.perform('speak', {
        message: message
      });
}

我的问题是表演的功能在哪里? 我试图将@perform修改为@performs,但该功能无法正常工作。

1 个答案:

答案 0 :(得分:1)

它是在rails中定义的。此处:https://github.com/rails/rails/blob/faa9a29fbbacc95e86c0ab3056a4443aa94e5530/actioncable/app/assets/javascripts/action_cable/subscription.coffee#L58-L60

class ActionCable.Subscription
  constructor: (@consumer, params = {}, mixin) ->
    @identifier = JSON.stringify(params)
    extend(this, mixin)

  # Perform a channel action with the optional data passed as an attribute
  perform: (action, data = {}) ->
    data.action = action
    @send(data)

App.cable.subscriptions.create获得的对象将具有此方法/功能。