代码之家  ›  专栏  ›  技术社区  ›  Marcos R. Guevara

客户端显示上次重新加载页面时连接的用户的数据

  •  0
  • Marcos R. Guevara  · 技术社区  · 7 年前

    我正在尝试创建一个“大厅”,您可以在其中加入聚会,其他用户接受列表中他想要的人,我正在使用 可操作电缆 铁轨上的红宝石

    输入请求显示上一个连接到接收数据的用户的信息,并且必须显示输入请求给我们的信息。( 不要用其他用户数据更改我们的视图! )

    这是JS文件(咖啡)

    App.join = App.cable.subscriptions.create "JoinChannel",
    
      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) ->
        $('#join_list_table').empty()
        this.render_list(data)
        $(".button-collapse").sideNav();
        $(".modal-trigger").click ->
          joinListId = $(this).attr('id')
          App.join.joinTheList joinListId
      # Called when there's incoming data on the websocket for this channel
    
      joinTheList: (listId) ->
        @perform "join", list_id: listId
        $('.modal').modal opacity: 0
      leave: ->
    
      render_list: (data) ->
        joinLists = data['joinLists']
        for joinList in joinLists
          joinings = joinList['joinings']
          if joinList['user']['id'] != data['current_user_id'] # here must say if the joinlist have the same id than the user
            $('#join_list_table').append(
              "<tr>
                <td>#{joinList['user']['name']}</td>
                <td>#{joinList['user']['email']}</td>
                <td>#{joinList['user']['language']}</td>
                <td>#{joinList['user']['country_code']}</td>
                <td>#{joinList['user']['price']}</td>
                <td>#{joinList['user']['currency']}</td>
                <td>
                  <a class='modal-trigger' id='#{joinList['list']['id']}' href='#modal#{joinList['list']['id']}' >
                   Join
                  </a>
                </td>
              </tr>
              ")
            $('main').append(
              "<div class='modal z-depth-0' id='modal#{joinList['list']['id']}'>
                <div class='modal-content'>
                  <div class='col s12'>
                    <ul id='joinings#{joinList['list']['id']}' class='collection with-header'>
                      <li class='collection-header'><h4>Join list for #{joinList['user']['name']}</h4></li>
                    </ul>
                  </div>
                </div>
              </div>")
    
            for joining in joinings
              $("#user_#{joining['user']['id']}_joining").remove();
              $("#joinings#{joinList['list']['id']}").append("<li id='user_#{joining['user']['id']}_joining'><a class='collection-item z-depth-0 center-align'>#{joining['user']['name']}</a></li>")
          else
            joinings = joinList['joinings']
            for joining in joinings
              $('#slide-out').append("<li id='user_#{joining['user']['id']}_joining'><a class='collection-item z-depth-0 center-align'>#{joining['user']['name']}</a></li>")
    document.addEventListener 'turbolinks:load', ->
      App.join.perform "render_all"
      $('#query').on 'keyup', ->
        value = $(this).val().toLowerCase()
        $('tr').filter ->
          $(this).toggle $(this).text().toLowerCase().indexOf(value) > -1
    

    那是Ruby文件

    class JoinChannel < ApplicationCable::Channel
      def subscribed
        unless current_user.join_list
          current_user.create_join_list
        end
        stream_from "join_channel"
        render_all
      end
    
      def unsubscribed
        if current_user.join_list.joinings
          current_user.join_list.joinings.destroy_all
        end
        current_user.join_list.destroy
        # Any cleanup needed when channel is unsubscribed
      end
    
      def join(data)
        joinlist = JoinList.find(data['list_id'])
        unless current_user.joining
          puts "esta creando"
          current_user.create_joining(join_list_id: joinlist.id)
        else
          puts "esta actualizando"
          current_user.joining.update(join_list_id: joinlist.id)
        end
        render_all
      end
      def render_all
        joinlistArr = []
        joinlists = JoinList.joins(:user)
        joinlists.each do |joinlist|
          joiningusers = []
          joinlist.joinings.each do |joining|
            joiningusers << {'user' => joining.user}
          end
          joinlistArr << {'list' => joinlist, 'user' => joinlist.user, 'joinings' => joiningusers}
        end
        puts "dame los joinings de la joinlist del current_user"
        puts current_user.join_list.joinings
        ActionCable.server.broadcast "join_channel", {joinLists: joinlistArr, current_user_id: current_user.id}
      end
    end
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Marcos R. Guevara    7 年前

    ActionCable.server.broadcast "join_channel", {joinLists: joinlistArr, current_user_id: current_user.id}
    

    JoinChannel.broadcast_to( 
          current_user, 
          joinLists: joinlistArr, 
          current_user_id: current_user.id 
        )