I have a basic chat application I’m building to play with liveview. I am using temporary_assigns
with the messages like this:
{:ok,
assign(socket, %{
messages: some_messages,
}), temporary_assigns: [messages: []]}
My messages div uses append
(this is for newly created messsages) like this:
<div id="chat-messages" phx-update="append" phx-hook="messages">
# render messages here
</div>
In my messages
hook, I want to listen for a scrollTop
of 0
to prepend earlier messages:
Hooks.messages = {
mounted() {
var $el = $(this.el)
var that = this;
$el.on("scroll", function (ev) {
if ($el.scrollTop() == 0) {
that.pushEvent("load_more", {})
}
})
},
In my handle_event
for the load_more
I have this:
new_msgs = some_func_to_get_messages_before_the_current_messages
{:noreply, update(socket, :messages, fn messages -> new_msgs ++ messages end)}
At this point socket.assigns.messages
would be []
because of temporary_assigns
. Current this doesn’t do anything, the new messages can’t be seen.
What’s the best way to handle this loading more messages on scroll?