CherryPoppins
I’m having trouble getting Alpinejs to “shadow” and “track” assigns in my live component. For instance using an example from Integrating Phoenix LiveView with JavaScript and AlpineJS I am not getting the expected behavior. My “assigns count” shows up and increments/decrements as it should, but I get nothing for the “alpine count”. Edit to add: I’m actually noticing now that the “alpine count” is actually rendering correctly for a split second on load, but disappears after. phx-update="ignore" does make it stay but I loose the ability to increment/decrement the count.
<div id="counter"
x-data="{count: <%= @count %>}">
<h1>The assigns count is: <span><%= @count %></span></h1>
<h1>The alpine count is: <span x-text="count"></span></h1>
<button phx-click="decrement" phx-target=<%= @myself %> %>> Decrement </button>
<button phx-click="increment" phx-target=<%= @myself %>> Increment </button>
</div>
Likewise, I have the below code where myHookOnWindow.myRenderData(data) runs fine and renders the data, but nothing happens with $watch('data', () => console.log('data changed')) when that data changes. The data is JSON in this case because it complains if I use a regular map or list.
<canvas
id="MyChart"
phx-hook="myHook"
x-data="{data: <%= @data %>}"
x-init="
myHookOnWindow.myRenderData(data)
$watch('data', () => console.log('data changed'))
"
>
</canvas>
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
c4710n
If you are using Alpine 3, it needs new method to integrate. Checkout Shadowed assigns with AlpineJS v3 and Phoenix LiveView 0.15.
CherryPoppins
Are you referring to adding:
if so, I currently have:
Would the new code replace my current
ifor be an additionalifin that method. Also is_x_dataStacksupposed to have two underscores instead of one on both sides of thex?CherryPoppins
So, I added the
iffrom my above comment to myonBeforeElUpdatedmethod and that did not work for me. I did try setting thex-dataproperty viax-initwith the assign I wanted to shadow and that seemed to work. Man, I wish the docs for Alpine stayed up to date and covered some of this fundamental stuff.julien1
If it’s any help, I was playing around with AlpineJS and Liveview for the first time today and had no issues with shadowing. I was actually going through the same article as you
But I did have to google some things for AlpineJS 3.
Here’s what I have in my app.js
Hopefully it will help?
Are there any errors in the JS console by the way?
CherryPoppins
No errors in the console. I tried your
ifin thedommethodonBeforeElUpdated, but can still only shadow assigns if I assign them tox-dataviax-initlike mentioned above. I guess that’ll have to work for now. Not sure if it matters but I’m onelixir 1.10.4-otp-22,erlang 22.3.4,{:phoenix_live_view, "~> 0.15"},{:phoenix, "~> 1.5.0", override: true},{:phoenix_html, "~> 2.10"}.