Hello everyone,
I am trying to hook some js to Phoenix LiveView (0.8.1)
I have a template like this
<a phx-click="toggle">Click</a>
<%= if @active do %>
<div id="VideoPlayer">Loading the player...</div>
<div phx-hook="Player">hook</div>
<% end %>
The toggle event switch the value of @active. and the hook is responsible for loading jwplayer in the VideoPlayer div.
The hook is like this
Hooks.Player = {
mounted() {
console.log("Player Mounted")
let jwplayer = window.jwplayer
let playerElement = document.getElementById("VideoPlayer")
if (playerElement) {
jwplayer.key = "my key"
let playerInstance = jwplayer(playerElement)
console.log(playerInstance)
let jwconfig = { /* jwplayer config object */}
playerInstance.setup(jwconfig)
}
},
updated() {
console.log("Player Updated")
},
destroyed() {
console.log("Player Destroyed")
}
}
It is working fine, JS is triggered correctly and the video player starts to play. My question is what happens when the player is removed?
I wanted to clean the player with this code in the destroyed hook event. This remove the player, but not the loading div. (console show correct message, but page show Loading the player…)
let jwplayer = window.jwplayer
let playerElement = document.getElementById("VideoPlayer")
if (playerElement) {
console.log("Player Destroyed")
jwplayer(playerElement).remove();
}
Why not cleaning works fine, but cleaning the player on destroy leave the div in place? Is it because I morphed DOM prior to remove?
Is it safe not to clean up on destroy?
Thanks for taking time