【问题标题】:how to update LiveComponent from LiveView Phoenix如何从 LiveView Phoenix 更新 LiveComponent
【发布时间】:2020-06-24 16:06:59
【问题描述】:

我有一个 LV 作为..

page_live.ex

defmodule EverdeployWeb.PageLive do
  use EverdeployWeb, :live_view

  def mount(_params, session, socket) do
    if connected?(socket), do: send(self(), :add_branches)
    {:ok, assign(socket, current_user: session["current_user"], evercam_server_branches: [], loading: true)}
  end

  def handle_info(:add_branches, socket) do
    {:noreply, assign(socket, evercam_server_branches: Github.branches("evercam-server2"), loading: false)}
  end
end

在 html.leex 我有

<section id="cd-timeline" class="cd-container">
  <%= if @loading, do: "loading", else: "" %> 
  <%= for branch <- @evercam_server_branches do %>
    <%= live_component @socket, Server, id: branch.sha, branch: branch %>
  <% end %>
</section>

ServerLiveComponent我有

<div class="cd-timeline-block">
  <div class="cd-timeline-img cd-picture">
    loading
  </div>

  <div class="cd-timeline-content">
    <h2><%= @branch.branch_name %></h2>
    <p>loading</p>
    <a href="#0" class="cd-read-more">Deploy</a>
    <span class="cd-date">loading</span>
  </div>
</div>

由于 LV 将挂载并将evercam_server_branches 发送到:live_view,我只显示branch_name,其他值将来自其他方法。

基本上是Github.branch(repo, branch),但我不确定在哪里调用此方法并更新所有正在加载到Server 上的值,我将更新它们将从branch/2 方法返回的值。

:live_view 加载时启动。我只想显示一个带有分支名称和加载图标的屏幕,然后在获取 branch/2 值后更新服务器。

我正在寻找方法,我怎么能在这里调用一些东西

defmodule Server do
  use Phoenix.LiveComponent

end

并更新Server 值?

【问题讨论】:

  • 您尝试过使用 PubSub 频道吗?
  • 不知道如何使用它们

标签: elixir phoenix-framework phoenix-live-view


【解决方案1】:

您可以在“mount”中调用带有计时器的更新函数,检查:

https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#connected?/1-examples

接下来,您需要通过send_update/2 更新每个组件 https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#send_update/2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多