waseigo

waseigo

How to JS.push and trigger a handled event on all items of @streams.something in a LiveView?

I have a LiveView that contains a text input field with phx-change tied to an event called “search” that searches locations in the database for the string in the text input field, and displays the results of @streams.locations using the table core component.

Each location item on the table has a “keep” action that gets triggered with JS.push, which adds the item to a “kept” list of locations in assigns. This way the user adds items from the search results’ stream (@streams.locations) to a list, before committing this “kept” list to the database with another button.

How do I implement an action that applies the “keep” action on the entire @streams.locations, i.e. a “keep all” action?

I tried a button with phx-click={JS.push("keep_all")} and then accessing socket.assigns.streams.locations within handle_event("keep_all", _params, socket), but IIUC the LiveView has no knowledge of what’s shown in the browser, as the stream items are not in memory.

I then tried something like this:

JS.push("keep_all", value: %{locations: Enum.with_index(@streams.locations)})

and got:

** (ArgumentError) 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.locations)`, you need to place the relevant information
within the stream items instead.

In other words: what is the idiomatic way of using JS.push to trigger an event that does something across all items in streams.something?

Most Liked

jswanner

jswanner

A simple approach would be to have a separate assign with all the IDs, and when someone clicks “keep all” use that all IDs assign

cmo

cmo

I’m not sure if you can. My first instinct would be to write a hook that queries those rows and sends a list of IDs to the backend.

Last Post!

KP123

KP123

I would apply a data-location-id attribute to each element in the list. Then I’d add a SelectionList hook to the list itself. The hook can listen for a keep-all event, iterate over all its children to build a list of IDs, then push the result to the server.

Edit: @cmo already suggested this

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement