简体   繁体   English

Phoenix Framwork LiveView 替换所有内容

[英]Phoenix Framwork LiveView replaces all content

I'm trying to build a live stock view with Phoenix LiveView.我正在尝试使用 Phoenix LiveView 构建实时股票视图。 I get the new data and it also gets updated in my Database.我得到了新数据,它也在我的数据库中得到更新。 The problem is that when there are eg 10 stocks and one gets updated only the updated one is shown.问题是,当有例如 10 只股票并且其中一只被更新时,只显示更新后的一只。 Here are the relevant code snippets.以下是相关的代码片段。 I'm using the PubSub System for sending/receiving events.我正在使用 PubSub 系统发送/接收事件。

 @impl true
  def mount(_params, _session, socket) do
    if connected?(socket), do: Stockview.subscribe()
    {:ok, assign(socket, :stocks, Stockview.list_stocks()), temporary_assigns: [stocks: []]}
  end
@impl true
  def handle_info({:stock_updated, stock}, socket) do
    {:noreply, update(socket, :stocks, fn stocks ->
      [stock | stocks]
    end)}
  end

When inspecting the "stocks" list it is empty.检查“库存”列表时,它是空的。

The object "stock" looks like this: object“股票”看起来像这样:

%EzStocks.Stockview.Stock{
  __meta__: #Ecto.Schema.Metadata<:loaded, "stocks">,
  ask_price: 178.38,
  id: 26,
  inserted_at: ~N[2020-10-08 01:26:20],
  isin: "US5949181045",
  name: "MICROSOFT CORP",
  sell_price: 178.2,
  updated_at: ~N[2020-10-08 10:52:18]
}

My template for rendering:我的渲染模板:

<h1>Listing Stocks</h1>

<%= if @live_action in [:new, :edit] do %>
  <%= live_modal @socket, EzStocksWeb.StockLive.FormComponent,
    id: @stock.id || :new,
    title: @page_title,
    action: @live_action,
    stock: @stock,
    return_to: Routes.stock_index_path(@socket, :index) %>
<% end %>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>ISIN / WKN</th>
      <th>Ask price</th>
      <th>Sell price</th>

      <th></th>
    </tr>
  </thead>
  <div id="st">
    <tbody id="stocks">
      <div id="stocks" phx-update="prepend">
        <%= for stock <- @stocks do %>
          <p id="<%= stock.id %>">
            <tr id="stock-<%= stock.id %>">
             <td><%= live_redirect stock.name, to: Routes.stock_show_path(@socket, :show, stock) %></td>
             <td><%= stock.isin %></td>
             <td><%= stock.ask_price %></td>
            <td><%= stock.sell_price %></td>
          </p>
            <td>
              <span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: stock.id %></span>
            </td>
        </tr>
      <% end %>
    </div>
    </tbody>
  </div>
</table>

<span><%= live_patch "New Stock", to: Routes.stock_index_path(@socket, :new) %></span>

I'd like to update the data and see the complete list, not just the updated object.我想更新数据并查看完整列表,而不仅仅是更新后的 object。

Thank you!谢谢!

Temporary Assigns Documentation 临时分配文件

I think it is because you marked the assigns as temporary, which means they will reset to a default value after each update, according to the documentation.根据文档,我认为这是因为您将分配标记为临时,这意味着它们将在每次更新后重置为默认值。 More info in the documentation link above.上面文档链接中的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM