Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Last active March 14, 2023 00:04
Show Gist options
  • Save Kedrigern/4672895 to your computer and use it in GitHub Desktop.
Save Kedrigern/4672895 to your computer and use it in GitHub Desktop.
Nette: Ajaxové ovládání komponenty
{* Component template *}
<div style="border-style: solid; border-width: 1px;">
<h4>Komponenta</h4>
<p>Jméno komponenty: {$control->name}</p>
{snippet com}
<p>Čas vykreslení:{$time|date:'%H:%M:%S'}</p>
{/snippet}
<p><a n:href="refresh!" class="ajax">REFRESH (invalidování)</a></p>
</div>
<?php
/**
* Componenta Com
*/
class Com extends Nette\Application\UI\Control
{
public function render()
{
$this->template->setFile(__DIR__ . '/Com.latte');
$this->template->time = date(DATE_RFC822);
$this->template->render();
}
public function handleRefresh()
{
if( $this->parent->isAjax() ) {
$this->invalidateControl('com');
} else {
// redirect může jen presenter, nikoliv komponenta
$this->parent->redirect('this');
}
}
}
{* HomepagePresenter:default *}
{block content}
{control com}
<h4>Presenter</h4>
<p>Čas: {$time|date:'%H:%M:%S'}</p>
<p>
| <a n:href="refresh!" class="ajax">Obnov komponentu</a>
| <a n:href="refreshDelay!" class="ajax">Obnov komponentu (čeká 3s)</a>
| </p>
<?php
class HomepagePresenter extends BasePresenter
{
public function renderDefault()
{
$this->template->time = date(DATE_RFC822);
}
public function handleRefresh()
{
if( $this->isAjax() ) {
$this['com']->invalidateControl();
} else {
$this->redirect('this');
}
}
public function handleRefreshDelay()
{
if( $this->isAjax() ) {
sleep(4);
$this['com']->invalidateControl();
} else {
$this->redirect('this');
}
}
protected function createComponentCom()
{
return new Com();
}
}
@Isigar
Copy link

Isigar commented Aug 29, 2018

Ahoj, jen taková připomínka, že $this['com']->invalidateControl() je již deprecated metoda a mělo by se místo toho používat redrawControl($name = null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment