actionCable of Rails5

actionCable of Rails5

在这个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函数在哪里? 我尝试将 @perform 修改为 @performs 并且该功能不起作用。

它在 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 获得的对象将具有此 method/function。