jeffrey

jeffrey

How to enforce Drab to reload full loop?

Hello.
I use Drab and I want just to change the value of assign inside of a loop.

I have this loop code:

<%= for page <- @holder.pages do %>
  <%= if page.id == @page_id do %>
    <tr>
      <td><%= page.title %></td>
    </tr>
  <% end %>
<% end %>

I change @page_id like this:

Drab.Live.poke(socket, page_id: id)

This function called when clicking on a button.
Everything works just fine when I place @page_id on some static element like h1.
But it doesn’t work inside of the loop.
Error: undefined function page/0

Can someone explain me why does it happen? I think Drab doesn’t fully reload loop - only if expression.

Marked As Solved

grych

grych

Creator of Drab

Unfortunately, it is not a bug. This is a limitation of Drab.Live’s local variables scope && the way how Drab is replacing parts of the page with the evaluated expression with new assign values.

When you’re poking the new version of assign @page_id, Drab tries to update it in every expression on a page, which contains that value. In this case, it is if page.id == @page_id, do:.... But in the particular moment it does not know the value of page variable - that’s why it shows undefined function page() while evaluating this expression.

It is done by purpose. I could search expressions for assigns in their do..end blocks, and re-render the the whole for comprehension while changing @page_id. And actually it was made like this before, but I realized that in the real world it provides to full reload of almost the whole page and more issues, like #42.

In this particular situation, when you poke @page_id, you really want to update the whole for compehension. One of the solution would be to put @page_id assign in the comprehension filter:

<%= for page <- @holder.pages, page.id == @page_id do %>
  <tr>
    <td><%= page.title %></td>
  </tr>
<% end %>

This moves @page_id to the for expression, and now, after you poke it, it re-evaluates the whole one.

I know it is not very intuitive, but so far I couldn’t find the better solution.

Anyway, please create an issue on github, this situation needs more explanation/docs and/or at least the better error description. Probably it should not even compile.

Docs: Drab.Live.EExEngine – drab v0.10.5

Also Liked

jeffrey

jeffrey

I tried to place this loop logic to commander function like this:

holder = peek(socket, :holder)
page = Enum.find(lesson.pages, fn(p) -> p.id == id end) id end)
poke(socket, page_title: page.title)

But I got nil instead of page.
I have no idea what’s wrong with this code. I tried a lot of tricks and workarounds.

OvermindDL1

OvermindDL1

Oh @grych, you around? I think the embedded order of data holding is a bit off. ^.^

@jeffrey You may want to report this on the Drab issue tracker. :slight_smile:

grych

grych

Creator of Drab

Hi @OvermindDL1, thanks for waking me up from my winter hibernation :wink:
Hi @jeffrey, I will take a look and will get back to you. It might be a limitation of how Drab is processing the code blocks (inside do…end), I am not sure if it is properly documented (here).

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New

We're in Beta

About us Mission Statement