简体   繁体   English

如何从 LiveView Phoenix 更新 LiveComponent

[英]how to update LiveComponent from LiveView Phoenix

I have a LV as..我有一个LV作为..

page_live.ex 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

and in html.leex I have在 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>

In the Server LiveComponent I haveServer LiveComponent我有

<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>

As the LV will mount and send evercam_server_branches to :live_view I am only displaying the branch_name the other values will come from an other method.由于 LV 将挂载evercam_server_branches并将其发送到:live_view我只显示branch_name其他值将来自其他方法。

Which is basically, Github.branch(repo, branch) But I am not sure where to call this method and update all values which are loading right now on Server and I will update them will values returned from the branch/2 method.这基本上是Github.branch(repo, branch)但我不确定在哪里调用此方法并更新所有正在加载的值,现在在Server上,我将更新它们将从branch/2方法返回的值。

As In start when :live_view load.:live_view加载时一样。 I just want to show a screen with a branch name and loading icons, and then after getting the branch/2 values update the Server.我只想显示一个带有分支名称和加载图标的屏幕,然后在获取branch/2值后更新服务器。

I am looking for way, how I can call something here我正在寻找方法,我怎么能在这里打电话

defmodule Server do
  use Phoenix.LiveComponent

end

and update the Server values?并更新Server值?

You may call update function with timer in "mount", check:您可以使用“mount”中的计时器调用更新 function,检查:

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

next, you'll need to update each component via send_update/2 https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#send_update/2接下来,您需要通过send_update/2 https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#send_update/2更新每个组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elixir phoenix LiveView 可折叠在更新时折叠 - Elixir phoenix LiveView collapsible collapses on update 如何在 Phoenix LiveView 中测试 handle_info/2? - How to test handle_info/2 in Phoenix LiveView? 如何让 Elixir/Phoenix LiveComponent 向其自身而不是其父级发送消息? - How can I make an Elixir/Phoenix LiveComponent send a message to itself, not its parent? 如何清除 Phoenix LiveView 表单中的文本区域? - How do you clear a textarea in a Phoenix LiveView form? Elixir / Phoenix LiveView:如何向Rollbar报告异常? - Elixir / Phoenix LiveView: How can I report exceptions to Rollbar? 如何将旧的 liveview 应用程序转换为新的 Phoenix 版本? - How to convert old liveview app to new Phoenix version? 如何将 LiveView 添加到现有的 Elixir/Phoenix 应用程序? - How can I add LiveView to an existing Elixir/Phoenix app? 如何从 Phoenix LiveView heex 模板调用 Elixir function 并传递来自套接字分配的数据 arguments? - How do I call an Elixir function from a Phoenix LiveView heex template passing arguments with data from socket assigns? 在挂载期间使用 LiveView 将值从客户端传递到 Phoenix 服务器 - Passing a value from client to Phoenix server using LiveView during mount 在 Phoenix Liveview 中,如何更改按钮标签以指示作业状态 - In Phoenix Liveview, how to change button labels to indicate job status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM