llaisdy
Dear All
nb a minimal working – or rather not working – example of my issue is here:
https://github.com/llaisdy/demo
The two relevant files are:
- demo/lib/demo_web/live/game_live.ex at master · llaisdy/demo · GitHub
- demo/assets/js/app.js at master · llaisdy/demo · GitHub
I am starting to implement a board game in elixir. Two pieces take turns to move around a square grid. I am using the HTML Drag and Drop API for moving the pieecs, and a phx-hook to identify the valid drop sites (i.e., on a given turn, which squares can a piece move to).
A moveable piece is a draggable div like this:
<div class="piece" id={@piece_id} draggable="true" ondragstart="dragStart(event)"></div>
Game-board squares are table cells like this:
<td id="cell-3-1" phx-hook="Drop">
(Using table for simplicity but will move to divs soon.)
The cells that have phx-hook="Drop" on initial page load are available drop sites and work as expected.
After each piece move, implicated cells have their phx-hook attribute updated (added or removed from the td). I can see the DOM is updated, but the changed cells are never mounted. The cells that have phx-hook="Drop" on initial page load remain the only available drop sites.
What am I missing or doing wrong?
Do I need to do something to force mount of updated elements?
Is there a simple LiveView board game I could consult for hints?
Many thanks
Ivan
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thiagomajesk
AFAIK, this is not possible, see this issue: phx-hook does not run in elements dynamically added to the DOM · Issue #2563 · phoenixframework/phoenix_live_view · GitHub.
I was doing something similar in the past but the idea of force-mounting hooks (to support dynamically added elements) was never implemented because of some security concerns. I’m hoping this will get revisited at some in the future though.
sodapopcan
You are mixing “regular” embedded JS calls with hooks here which I can’t imagine is the intended use.
Can you have something more like this where everything is being managed by one hook?
This is more or less how I’ve done drag-and-drop in the past complete with dynamically added elements (though I was using a library).
llaisdy
Hi, thanks for your comment! I link to that issue from my github README
especially Chris McCord’s & Jose Valim’s comments from here on, eg:
I am adding these hooks on the server – in a LiveView – I understood your issue to be adding hooks client-side.
llaisdy
[1]
The piece is moved from (3,1) to (3,2), and the
data-drop-targetattribute is updated on bothtds.[2]
Could the
Boardhook pick that up?sodapopcan
Ya, you essentially delegate to JS here, which maybe you are trying not to do? Again, I’ve only ever used a library, so it’s a pretty simple set up of setting up the library hooks in the Phoenix JS Hooks (ugh, so many hooks!) and pushing updates to the server
For example, here my entire phx hook using SortableJS for a fairly old project (but is the only one top of mind):
The point being that the JS takes care of watching for new items. In my example, that’s all hidden away in Sortable but that’s the idea.
Otherwise, ya, creating an element with a hook server side should call mount, I think your issue was just mixing hooks with non-hooks (I think?) but it looks like you have more insight now!
codeanpeace
To add on to this, the TodoTrek showcase repo has a working example of LiveView and Sortable that supports Trello/kanban-style dragging and dropping cards between lists.
llaisdy
Excellent info, thanks. Yes – trying to avoid javascript
Will try it out and report back soon.
llaisdy
Thanks v much, I’ll give it a look. I have used Sortable with a django app. Will report back with results/failures soon.