jeffrey

jeffrey

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.

Showing Posts 1 to 9

jeffrey

jeffrey OP

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).

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

jeffrey

jeffrey OP

Ok. Thanks for explaining!

grych

grych

Creator of Drab

I’ve got the idea on how to make such things easier to maintain for a developer. What about if you can leave your code as it is, and ensure the parent comprehension is refreshed by just updating both assigns?

poke socket, page: peek(:page), page_id: new_page_id

(or even introduce new API)

poke socket, page_id: new_page_id, refresh_assigns: [:page]

In the current version poking both assigns will generate the same error, because Drab will try to update @page_id anyway, even if it is completely pointless as it is inside the for comprehension, which is updated anyway.

Drab should be aware of such cases and update only the parent statement. It would be also a performance improvement.

jeffrey

jeffrey OP

Btw I tried to update both assigns when I was looking for solution. So I think it’s intuitive.
But I think new API for updating additional assigns even more intuitive.

grych

grych

Creator of Drab

Cool. I will do that, one more small step for Drab to be better!

grych

grych

Creator of Drab

It is solved now, the change will be included in 0.7.1.

Solution for your case:

poke socket, holder: peek(socket, holder), page_id: new_page_id

I did not introduce a new API for poke, as I don’t want to add a special options like refresh_assigns. You may have an assign named @refresh_assigns in you page :slight_smile:

If you have an idea for a good API for this case, please do not hesitate.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews