Giter VIP home page Giter VIP logo

Comments (16)

denisw avatar denisw commented on September 17, 2024 2

But won't you still have the problem that the re-rendered inputs possibly lose focus / the cursor position? It's impossible to completely restore an input's state after replacing it.

These are the kinds of re-rendering provlems that drove me crazy when I was still using Backbone.js :)

It seems that the only real solution here is using a virtual DOM implementation (there are wuite a few out there not bound to a framework), or very exact data binding.

from drab.

grych avatar grych commented on September 17, 2024 1

Analysis

In the template there are two functions(macros) which uses assign:
form_for (@conn and @list - notice that @list is also inside it)
for (@list)

That means, when you poke the @list, both of the functions are re-evaluated, so everything in the form inputs is replaced with the default. The same is about the csfr token.

Workaround for now: do not use form_for, sorry :( but create form by hand, <form></form>

I have no idea (yet) how to solve it. I even donโ€™t know if it is solvable, because this behaviour is a Drabโ€™s core. Maybe I will need to add it to the limitations list.

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 17, 2024 1

You could always replace form_for with your own version?

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 17, 2024 1

Sounds almost like you need to make a virtual dom and 'patch' it onto the main instead of swapping. ^.^

Hmm, now that would be an interesting idea... A Drab.VDom library...

from drab.

eirikra avatar eirikra commented on September 17, 2024 1

Ah, my apologies. I confused two different issues. I need to double check this, however, but โ€“ if I remember correctly โ€“ when using protect from forgery forms are not validated because the csfr-token is changed after poke.

from drab.

grych avatar grych commented on September 17, 2024 1

Actually, it is not very complicated to fix.
Before update, I am going to collect all values on <input> fields, by name attribute. After the update, I will fill up the corresponding values with the saved ones.
This should fix the csfr issue as well, as it is stored in the hidden input.

from drab.

grych avatar grych commented on September 17, 2024 1

@eirikra
could you please test again with current master? I hope I solved the issue.

from drab.

grych avatar grych commented on September 17, 2024 1

Fixed in v0.6.0-pre.1

from drab.

grych avatar grych commented on September 17, 2024

Unfortunatelly, not so easy. It affects any <input> which is inside the expression, it doesn't have to be form_for.

<%= if (something) do %>
  <%= @some_assign %>
  <input>
<% end %>

Updating @some_assign with poke will re-render the whole if expression (as this assign is inside that expression). So anything user put in the <input> will disapear.

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 17, 2024

Hmm, I really need to try Drab.Live in my setup. Just about everything is inside (usually multiple layers of) if/for/case and more...

from drab.

grych avatar grych commented on September 17, 2024

<input> values must survive refreshing the parent expression. Otherwise it is counter-intuitive. Drab.Live should be a black magic: things should just happen.

from drab.

eirikra avatar eirikra commented on September 17, 2024

If I'm not mistaken, we found that another workaround (if you don't want to build the form manually) is to not use the :protect_from_forgery plug. This might, however, not be something you'd necessarilty want to do...

from drab.

grych avatar grych commented on September 17, 2024

@eirikra but it still resets the values of <input>, doesn't it?

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 17, 2024

It seems that the only real solution here is using a virtual DOM implementation (there are wuite a few out there not bound to a framework), or very exact data binding.

Technically Drab Live's special EEX handler is making a kind of virtual DOM. :-)

from drab.

grych avatar grych commented on September 17, 2024

Yes, there is a kind of virtual DOM there, under heavy rebuild now.

The root of the issue is how Drab searches for assigns in the expression. Compiled template like:

<%= if true do %>
  <%= @assign %>
<% end %>

contains two expressions (if and assign), and both of them contain this assign. When poking the @assign, drab re-renders both of them, because it is too stupid to recognize that it should be only done in the second one.

One of solutions would be to allow developer to choose where not to drab the expression with the custom marker:

<%/ if true do %>
  <%= @assign %>
<% end %>

For this, I made this PR elixir-lang/elixir/pull/6281. But it is not enough, it should be more clever choosing where to eval and replace expressions.

from drab.

OvermindDL1 avatar OvermindDL1 commented on September 17, 2024

it should be more clever choosing where to eval and replace expressions.

Such as by threading an environment through the calls to state what is required and what is not and so forth?

from drab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.