Skip to content

Dealing with sorted data in a stream #109

@miczed

Description

@miczed

I've been playing around with hotwire for the last couple of days and so far I really like it, great work! 👍 However, I couldn't yet figure out how to deal with data that is sorted (e.g. has an order field with a numeric value). So when an element is created instead of just adding / prepending it to the turbo frame, it should be added as the nth element in the list.

I was thinking of something that could work similar to this:

<turbo-stream action="append" target="messages" index="5">
  <template>
    <div id="message_1">
      This div will be appended to the element with the DOM ID "messages" as the 5th element.
    </div>
  </template>
</turbo-stream>
<turbo-stream action="update" target="message_2" index="3">
  <template>
    <!-- The contents of this template will replace the
    contents of the element with ID "message_2" and change its position to the third place. -->
    1
  </template>
</turbo-stream>

Another approach could consist of leveraging Node.insertBefore() in combination with a new StreamAction called insertBefore:

<turbo-stream action="insertBefore" target="messages" reference="message_2">
  <template>
    <div id="message_1">
      This div will be appended to the element with the DOM ID "messages" before the element with the DOM ID "message_2".
    </div>
  </template>
</turbo-stream>

Do you plan to support more advanced use cases like this? Is there a simple way to change the behavior of the StreamActions (i.e. to switch out this.targetElement?.append(this.templateContent) with something more sophisticated)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions