blazejcm
How to get items' indices when using streams?
Hi everyone!
I have a list of items that I display in Phoenix LiveView using streams.
Currently, I am working on the “Clone” feature. any item can be cloned and the new (cloned) item is supposed to appear right below the item being cloned.
I want to use stream_insert(..., at: position) but for this to work, I need to know the relative position of every item on the list.
I tried a few variations of the following in my view:
:for={ {{dom_id, entry}, position} <- Enum.with_index(@streams.entries) }
but I get the following error:
streams can only be consumed directly by a for comprehension. If you are attempting to consume the stream ahead of time, such as with
Enum.with_index(@streams.entries), you need to place the relevant information within the stream items instead.
I have no idea how to proceed in a way that would be idiomatic. I am sure I can hack my way out of this - but there’s got to be “the right way” to do it…
Thanks in advance!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First Post!
Hermanverschooten
The only way I have found to do this, is to “enrich” your item with it’s index.
You cannot just use
Enum.with_index/1becausestreamrequires your item to be aMapwith an:id.Most Liked
Hermanverschooten
We are talking about a
Phoenix.LiveViewstream, notElixir.Stream.Confusing that they chose the same term.
Last Post!
blazejcm
But I was thinking… I’ll try accessing the items in my stream in
handle_event, finding the right item and figuring out its index. Maybe it will work.Cheers!