如何知道事件是否已触发?看起来似乎没有。

huangapple go评论54阅读模式
英文:

How do I know if an event was fired? Looks like it didn't

问题

I have a UserShow component which shows the user's data (name, email, etc.) and also the actions he made in the site via relations (messages written, likes given, etc.). Now I want to allow the user to change some of his data (name, email, etc.), so I wrote another component called UserEdit here is the form, validation, etc. It's working fine.

Since I'm using Livewire/Alpine I have some logic like

<div x-data="{ isEditing = @entangle('isEditing) }">
    <section x-show="!isEditing" />
        ...All user data...
        <button x-on:click="isEditing=true">
    </section>

    <section x-show="isEditing">
        <livewire:user.user-edit :user="$user" />
    </section>
</div>

So this works - it opens the component where the user will edit his data. In the component by the form's end, I have two buttons:

<div>
    <button type="submit">update</x-gui.button>
    <button type="button" wire:click="canceled">Cancel</button>
</div>

The canceled function in the component is:

public function canceled() {
    Log::info('Canceling Edit');
    $this->emitUp('canceled');
}

Now, in the parent element I have the listeners and the function defined as:

protected $listeners = ['canceled' => 'restart'];

public function restart() {
    Log::info('canceled received');
}

When I click the cancel method in the component, I see the first log "Canceling Edit," but I don't see the "canceled received" message.

Am I doing something wrong? How do I know if the "canceled" event is even being fired?

英文:

I have a UserShow component which shows the user's data (name, email, etc.) and also the actions he made in the site via relations (messages written, likes given, etc.). Now I want to allow user to change some of his data (name, email, etc.), so I wrote another component called UserEdit here is the form, validation, etc. It's working fine.

Since I'm using Livewire/Alpine I have some logic like

&lt;div x-data=&quot;{ isEditing = @entangle(&#39;isEditing) }&quot;&gt;
    &lt;section x-show=&quot;!isEditing&quot; /&gt;
        ...All user data...
        &lt;button x-on:click=&quot;isEditing=true&quot;&gt;
    &lt;section&gt;

    &lt;section x-show=&quot;isEditing&quot;&gt;
        &lt;livewire:user.user-edit :user=&quot;$user&quot; /&gt;
    &lt;/section&gt;
&lt;/div&gt;

So this works - it opens the component where the user will edit his data. In the component by the form's end, I have two buttons:

&lt;div&gt;
  &lt;button type=&quot;submit&quot;&gt;update&lt;/x-gui.button&gt;
  &lt;button type=&quot;button&quot; wire:click=&quot;canceled&quot;&gt;Cancel&lt;/button&gt;
&lt;/div&gt;

The canceled function in the component is:

public function canceled() {
  Log::info(&#39;Canceling Edit&#39;);
  $this-&gt;emitUp(&#39;canceled&#39;);
}

Now, in the parent element I have the listeners ans the function defined as:

protected $listeners = [&#39;canceled&#39; =&gt; &#39;restart&#39;];

public function restart() {
  Log::info(&#39;canceled received&#39;);
}

When I click the cancel method in the component, I see the first log "Canceling Edit", but I don't see the "canceled received" message

Am I doing something wrong? How do I know if the "canceled" event is even being fired?

答案1

得分: 0

我发现了发生的事情,这是一个非常奇怪的行为,所以调试花了很长时间。

在这个非常具体的组件中的问题是,它在一个foreach循环中显示了所有的消息,像这样:

<div x-data="{ isEditing = @entangle('isEditing) }">
    <section x-show="!isEditing" />
        ...所有用户数据... <=== 精确地在这里!!!
        <button x-on:click="isEditing=true">
    <section>

    <section x-show="isEditing">
        <livewire:user.user-edit :user="$user" />
    </section>
</div>

所有用户数据都有一个 @foreach() 循环,用于渲染另一个组件:

@foreach($user->messages as $message)
    <livewire:message-card :message="$message" />
@endforeach

问题是这个 Livewire 组件没有被标记,这导致了各种奇怪的问题发生,将行更改如下,解决了问题:

<livewire:message-card :message="$message" :key="$message->id" />

所以,这个警示故事是:无论组件是否只用于显示信息而不对其进行操作,始终在循环内为它们标记键

英文:

I found what happened, and it was a very strange behaivour, so it tooks ages to debug.

The problem in this very specific component is that it was displaying all the messages inside a foreach, like:

&lt;div x-data=&quot;{ isEditing = @entangle(&#39;isEditing) }&quot;&gt;
    &lt;section x-show=&quot;!isEditing&quot; /&gt;
        ...All user data... &lt;=== PRECISELY HERE!!!
        &lt;button x-on:click=&quot;isEditing=true&quot;&gt;
    &lt;section&gt;

    &lt;section x-show=&quot;isEditing&quot;&gt;
        &lt;livewire:user.user-edit :user=&quot;$user&quot; /&gt;
    &lt;/section&gt;
&lt;/div&gt;

The all user data had a @foreach() that was rendering another component:

@foreach($user-&gt;messages as $message)
    &lt;livewire:message-card :message=&quot;$message&quot; /&gt;
@endforeach

The problem was this livewire component wasn't being keyed, and this caused all kind of strange things to happen, chaging the line as below, solved the problem

&lt;livewire:message-card :message=&quot;$message&quot; :key=&quot;$message-&gt;id&quot; /&gt;

So, the cautionary tale is: Always key your components inside a loop, even when they are just for displaying information, and not to be acted upon them

huangapple
  • 本文由 发表于 2023年5月22日 02:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定