首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React - Rails 5动作电缆

React - Rails 5动作电缆
EN

Stack Overflow用户
提问于 2018-02-19 06:38:49
回答 1查看 127关注 0票数 0

我现在已经尝试了多种方法,但似乎无法获得要广播给客户端的消息。

在这个应用程序中,我获得了一个执行通道订阅的react组件。我的连接应用程序的控制台输出显示在我的日志中,但接收到的只是没有被调用。

我的React组件:

代码语言:javascript
复制
  # app/assets/javascripts/components/records.js.coffee
  App.record = null
  @Records = React.createClass
    getInitialState: ->
      @setupSubscription()
      records: @props.data
    getDefaultProps: ->
      records: []
    setupSubscription: ->
      App.record = App.cable.subscriptions.create "RecordChannel",
        connected: ->
          console.log "connected to RecordChannel"
          # Timeout here is needed to make sure Subscription
          # is setup properly, before we do any actions.
          #setTimeout: ->
          #  this.perform 'follow',
          #               {message_id: this.message_id}
          #  ,1000
        received: (data) ->
          console.log "data received"
          @addRecord(data)
      #record_subs = App.record
    addRecord: (record) ->
      records = React.addons.update(@state.records, { $push: [record] })
      @setState records: records
    deleteRecord: (record) ->
      index = @state.records.indexOf record
      records = React.addons.update(@state.records, { $splice: [[index,1]] })
      @replaceState records: records
    updateRecord: (record,data) ->
      index = @state.records.indexOf record
      records = React.addons.update(@state.records, { $splice: [[index,1,data]] })
      @replaceState records: records
    credits: ->
      credits = @state.records.filter (val) -> val.amount >= 0
      credits.reduce ((prev, curr) ->
        prev + parseFloat(curr.amount)
      ), 0
    debits: ->
      debits = @state.records.filter (val) -> val.amount < 0
      debits.reduce ((prev, curr) ->
        prev + parseFloat(curr.amount)
      ), 0
    balance: ->
      @debits() + @credits()
    render: ->
      React.DOM.div
        className: 'records'
        React.DOM.h2
          className: 'title'
          'Records'
        React.DOM.div
          className: 'row'
          React.createElement AmountBox, type: 'success', amount: @credits(), text: 'Credit'
          React.createElement AmountBox, type: 'danger', amount: @debits(), text: 'Debit'
          React.createElement AmountBox, type: 'info', amount: @balance(), text: 'Balance'
        React.createElement RecordForm, handleNewRecord: @addRecord
        React.DOM.hr null
        React.DOM.table
          className: 'table table-bordered'
          React.DOM.thead null,
            React.DOM.tr null,
              React.DOM.th null, 'Date'
              React.DOM.th null, 'Title'
              React.DOM.th null, 'Amount'
              React.DOM.th null, 'Actions'
          React.DOM.tbody null,
            for record in @state.records
              React.createElement Record, key: record._id["$oid"], record: record, handleDeleteRecord: @deleteRecord, handleEditRecord: @updateRecord

不确定你还需要看哪段代码,请让我知道,我会把它贴出来。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-20 05:44:16

我终于找出了我的应用程序中的错误所在。我的订阅方法在堆栈中是错误的。原来我在订阅中使用的是stream_for而不是stream_for

代码语言:javascript
复制
class RecordChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_for ("record_#{current_user['info']['nickname']}_channel")
  end

代码语言:javascript
复制
class RecordChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_from ("record_#{current_user['info']['nickname']}_channel")
  end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48857369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档