[英]why phoenix liveview update/assign message make client contenteditable value revert?
With Phoenix live view document I add a live page for writing a realtime form app.使用 Phoenix 实时视图文档,我添加了一个用于编写实时表单应用程序的实时页面。 There is very simple demo:
有一个非常简单的演示:
<h2>Hello</h2>
Counter is222: <%= @counter %>
<hr>
<button phx-click="dec">-</button>
<button phx-click="inc">+</button>
<table border="1">
<tr>
<th contenteditable="true">Month</th>
<th>S1</th>
<th>S2</th>
</tr>
<tr>
<td contenteditable="true">January</td>
<td contenteditable="true">$100</td>
<td contenteditable="true">$10220</td>
</tr>
</table>
Server side code (just like document):服务器端代码(就像文档一样):
defmodule TicTacWeb.MemberSchedulerLive do
use Phoenix.LiveView
def render(assigns) do
TicTacWeb.PageView.render("member_scheduler.html", assigns)
end
def mount(_, socket) do
{:ok, assign(socket, %{counter: 100})}
end
def handle_event("inc", _, socket) do
{:noreply, update(socket, :counter, fn(x) -> x + 1 end)}
end
def handle_event("dec", _, socket) do
IO.puts("item")
{:noreply, update(socket, :counter, fn(x) -> x - 1 end)}
end
end
The problem is <td contenteditable
value will be revert after I click -
or +
emit a message to server.问题是
<td contenteditable
值将在我单击后恢复-
或+
向服务器发送消息。
-
or +
affect <td>
's value?-
或+
会影响<td>
的值? Is't minimize change modified dom data? Thanks!谢谢!
UPDATE更新
After add contenteditable as data model, it was still not work, such as:添加 contenteditable 作为数据模型后,还是不行,比如:
1. html snippet 1. html 片段
....
<td contenteditable="true">$100</td>
<td contenteditable="true" phx-blur="somedata"><%=@somedata%></td>
</tr>
</table>
...
def mount(_, socket) do
{:ok, assign(socket, %{counter: 100, somedata: "$001"})}
end
The @somedata
also revert to $001 if click -
or +
.如果单击
-
或+
, @somedata
也会恢复为 $001。
why
-
or+
affect<td>
's value?为什么
-
或+
会影响<td>
的值?
It's vice versa.反之亦然。 They do not affect
<td>
's value, on the contrary, it sends the diff between the previous state and the updated state and the frontend applies this diff .它们不影响
<td>
的值,相反,它发送前一个状态和更新状态之间的差异,前端应用这个差异。
LiveView
has no idea about your local changes with the contenteditable="true"
and hence it resets everything to its original state, save for :counter
that gets updated. LiveView
不知道您使用contenteditable="true"
所做的本地更改,因此它将所有内容重置为其原始状态,除了:counter
会更新。 To support contenteditable="true"
you need to make these <td>
'sa part of data model so that LiveView
is aware about the changes in there.要支持
contenteditable="true"
您需要将这些<td>
设为数据模型的一部分,以便LiveView
了解其中的更改。
I finally solve the problem by two step:我终于通过两步解决了这个问题:
add phx-update='ignore'
and the contenteditable will not be update!添加
phx-update='ignore'
并且 contenteditable 将不会更新!
In this stage, it's <td contenteditable="true" phx-update='ignore'>$10220</td>
在这个阶段,它是
<td contenteditable="true" phx-update='ignore'>$10220</td>
update phoenix_live_view to 0.4.1 which was github: phoenixframework/phoenix_live_view
(locked commit is: 73e9a695eff1d7d21e4cb34ea9894835b3a2fa32) and the old version seems not support phx-update
将 phoenix_live_view 更新为 0.4.1,即
github: phoenixframework/phoenix_live_view
(锁定提交为:73e9a695eff1d7d21e4cb34ea9894835b3a2fa32),旧版本似乎不支持phx-update
Hope it also could be help you.希望它也可以帮助你。
Thanks @Aleksei Matiushkin, I will dive into liveview diff algorithm.谢谢@Aleksei Matiushkin,我将深入研究实时视图差异算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.