Nittro, invalidace a flash message
- Damo
- Člen | 56
Řeším jednu věc, a to, že mi nechce probublat flash message do komponenty, z ajaxového resultu na handle.
Resp. invaliduji jen dva snippety, které se mi překreslí a do jiného snippetu v komponentě potřebuji poslat jen flash message, nesmí se jinak překreslit.
@layout.latte
...
<div n:snippet="content" class="wrapper">
{include content}
</div>
...
default.latte (snippety navbar a main se mi prekresli, potrebuji do control xWindow poslat flashmessage)
{block content}
<div class="main">
{snippetArea mc}
<nav n:snippet="navbar" class="navbar navbar-expand">
...
</nav>
...
<main n:snippet="main" class="content draggable-container content-overflow-auto">
...
</main>
{control xWindow}
{/snippetArea}
...
</div>
{/block content}
xWindow.latte
<div
n:snippet="x-windows"
n:dynamic="x-window-\d+"
data-dynamic-element="div.x-window"
class="x-windows-group nittro-snippet-container"
>
{foreach $windows as $window}
...
<div n:snippet="x-window-flash">
{foreach $flashes as $flash}
<div class="alert alert-{$flash->type|noescape} alert-dismissible" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<div class="alert-message">
{$flash->message}
</div>
</div>
{/foreach}
</div>
{control xWindowContent, $window}
...
{/foreach}
...
</div>
handle
public function handleChangeLocale(string $locale): void {
$this->translatorSessionResolver->setLocale($locale);
$this->flashMessage("Zpráva do x-window komponenty" ,"info");
$this->postGet('this');
$this->redrawControl('navbar');
$this->redrawControl('main');
$this->redrawControl('mc');
$this->redrawControl('x-window-flash');
}
ajax result
flashes: {flash: [{message: "Zpráva do x-window komponenty", type: "info"}],…}
flash: [{message: "Zpráva do x-window komponenty", type: "info"}]
xWindow-flash: null
xWindow-xWindowContent-flash: null
postGet: true
snippets: {,…}
snippet--main: "..."
snippet--navbar: "..."
state: {locale: "cz"}
url: "/?locale=cz"
Je to hodně zanořené, tak nevím kde se ztrácím a proč mi to nejde. Pokud tu flashmessage vytknu do jednoho z snippetu main nebo navbar tak se zobrazi, ale samozrejme na jinem miste než chci.
Editoval Damo (5. 1. 2022 8:33)
- Damo
- Člen | 56
Tak jo, určité řešení jsem našel.
control je potreba obalit do snippetArea
{block content}
<div class="main">
<nav n:snippet="navbar" class="navbar navbar-expand">
...
</nav>
...
<main n:snippet="main" class="content draggable-container content-overflow-auto">
...
</main>
{snippetArea windows}
{control xWindow}
{/snippetArea}
...
</div>
{/block content}
v handlu, zavolat redrawControl na snippetArea a ten vyvolá poté render v komponentě xWindow. Ale tady pozor, render neprijima zadny parametry, takze render nevi, jestli jde o bezny vypis nebo o redraw, kde je potreba invalidovat nejaky snippet uvnitr dané komponenty. Takže v handlu si naplním seznam child snippetu pro invalidaci
public function handleChangeLocale(string $locale): void {
$this->translatorSessionResolver->setLocale($locale);
$this->postGet('this');
$this->redrawControl('navbar');
$this->redrawControl('main');
// snippetarea and child snippet for redraw xWindow Component
$this->redrawControl('windows');
$this->template->redrawControl = ["langFlash"];
}
a v renderu componenty si jej vyctu pro invalidaci
public function render(): void {
// invalid all defined child snippets
if (isset($this->presenter->template->redrawControl)) {
foreach ($this->presenter->template->redrawControl as $control) {
$this->redrawControl($control);
}
unset($this->presenter->template->redrawControl);
}
$this->template->setFile(__DIR__ . "/XWindows.latte");
$this->template->windows = $this->getWindows();
$this->template->render();
}
tohle řešení funguje