F
Filamentβ€’3mo ago
frame

Closing tab after saving changes from EditResource

I am opening an EditResource page in a new tab from the front end. Is it possible to also close the tab once the user has saved changes? I tried dispatching an event from getSaveFormAction action's after method but I can't get the event to fire. Just "livewire:init" shows up in console. πŸ€”
protected function getSaveFormAction(): Action
{
return Action::make('save')
->submit('save')
->after(function () {
$this->dispatch('close-page');
});
}
protected function getSaveFormAction(): Action
{
return Action::make('save')
->submit('save')
->after(function () {
$this->dispatch('close-page');
});
}
->renderHook(PanelsRenderHook::BODY_END, fn (): HtmlString => new HtmlString(<<<'HTML'
<script>
window.addEventListener("livewire:init", function () {
console.log("livewire:init");
Livewire.on("close-page", function () {
console.log("livewire close page");
window.close();
});
});
</script>
HTML))
->renderHook(PanelsRenderHook::BODY_END, fn (): HtmlString => new HtmlString(<<<'HTML'
<script>
window.addEventListener("livewire:init", function () {
console.log("livewire:init");
Livewire.on("close-page", function () {
console.log("livewire close page");
window.close();
});
});
</script>
HTML))
Cancel was easier:
protected function getCancelFormAction(): Action
{
return Action::make('cancel')
->color('gray')
->extraAttributes([
'x-on:click' => 'window.close()',
]);
}
protected function getCancelFormAction(): Action
{
return Action::make('cancel')
->color('gray')
->extraAttributes([
'x-on:click' => 'window.close()',
]);
}
Solution:
Ok I sort-of managed to do this using ```php protected function handleRecordUpdate(Model $record, array $data): Model {...
Jump to solution
3 Replies
Solution
ChesterS
ChesterSβ€’3mo ago
Ok I sort-of managed to do this using
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record = parent::handleRecordUpdate($record, $data);
$this->dispatch('close-page');
return $record;
}
protected function handleRecordUpdate(Model $record, array $data): Model
{
$record = parent::handleRecordUpdate($record, $data);
$this->dispatch('close-page');
return $record;
}
For some reason, the event was not dispatched at all either using your example or
protected function afterActionCalled(): void
{
$this->dispatch('close-page');
}
protected function afterActionCalled(): void
{
$this->dispatch('close-page');
}
BUT you must be opening the tab with JS (I'm assuming that what you mean by "I am opening ... a new tab from the front end" Anyway, hope it helps
ChesterS
ChesterSβ€’3mo ago
Obviously, we could both be doing something wrong 🀣
frame
frameOPβ€’3mo ago
Wow, thanks thats amazing.😍 the dispatch from handleRecordUpdate does indeed work.

Did you find this page helpful?