【问题标题】:actionCable of Rails5actionCable of Rails5
【发布时间】:2018-07-16 11:15:46
【问题描述】:

在这个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,该功能不起作用。

【问题讨论】:

  • “我试图将@perform 修改为@performs” - 为什么?你对会发生什么的假设是什么?
  • @SergioTulentsev 我只想知道执行功能在哪里

标签: ruby-on-rails actioncable


【解决方案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得到的对象,会有这个方法/功能。

【讨论】:

    猜你喜欢
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2016-11-30
    相关资源
    最近更新 更多