0

我在聚合物应用程序中选择 ds 列表时遇到问题。我有一个有效的聊天标题列表(实现为深度流记录),应该用于选择匹配的聊天历史记录(实现为包含聊天消息作为记录的深度流列表)。

   <div class="chatlist">
     <!-- This is the list of chats -->
      <paper-menu selected="[[chatlist]]">
        <paper-item>
          <paper-input label="New Chat:" id="chatName" on-keydown="setChatName"></paper-input>
        </paper-item>
        <template 
        is="dom-repeat" 
        items="[[todos]]" 
        as="recordId">
        <div role="listbox">
          <chat-names
            name="[[recordId]]">
          </chat-names>
        </div>
        </template>
      </paper-menu>
  </div>

  <!-- this is the chat history -->
  <iron-pages
    selected="[[chatlist]]"
    attr-for-selected="chatView"
    fallback-selection="chatView404"
    role="main">    
      <template 
        is="dom-repeat" 
        items="[[todos]]" 
        as="recordId">
          <chat-view
            chatView="[[???]]"
            name="[[recordId]]">
          </chat-view>
      </template>
  </iron-pages>

所以这是我的问题:虽然聊天列表工作正常,但我不知道如何将聊天本身的选择连接到匹配聊天历史的显示。

聊天标题的创建发生在 paper-inputon-keydown="setChatName"函数中,如下所示:

setChatName: function (e) {
    if (e.which === 13) {
      var recordId =  'polymer-example/' + this.ds.getUid();
      var todo = this.$$( '.new-record-input' ).value;
      var todoRecord = this.ds.record.getRecord( recordId );
      var todoList = this.ds.record.getList( this.name );

      todoRecord.set( { name: todo, checked: false } )
      todoRecord.whenReady( function() {
        todoList.addEntry( recordId );
      } );
      this.$.chatName.value = '';
    }
  },

我现在如何不仅可以设置聊天名称本身的记录,还可以设置包含聊天记录的 ds-list?并且: ds-list 的哪些属性有用(例如,id?名称?)用作选择它的属性?

对不起,很长的问题,每一个答案都非常感谢!

4

1 回答 1

1

First, forgive me, but I'm confused by the examples you've given. The second code snippet appears to be example code for a todo app. It would help immensely if you used descriptive variable names.

You appear to have code that allows a user to create a new chat name, where a record is created called 'polymer-example/$RANDOM_ID' (i'd suggest using something like 'chat-details/$RANDOM_ID' instead) with the value { name: $CHAT_NAME, checked: false }. The record 'polymer-example/$RANDOM_ID' is then added to a list with some polymer property You have a deepstream list of names where each name corresponds to a deepstream record containing the name of a chat.

Supposing that you would like to store a reference to the currently selected chat, I suggest that either you store the corresponding chat id (this is called recordId in the example provided) as a property, or instead you could store a reference to the record itself.

For storing the chat history i'd suggest creating another list (called e.g. 'chat-history/$CHAT_ID') for each chat. Then for each new message, you can create a corresponding record ('chat-message/$RANDOM_ID') and use that to store the message content, name of the sender, time etc. Now add the name of your message record to the chat-history list.

Selecting the correct chat history is then just a case of getting the currently selected chat(chatId), getting the list 'chat-history/' + chatId which contains a list of message ids, then fetching the record 'chat-message/' + messageId.

于 2017-02-22T20:52:51.020 回答