Skip to content

Instantly share code, notes, and snippets.

@Kcko
Forked from Kedrigern/Com.latte
Created April 17, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/3b8cf9aed62ad6065d2f1692906e652b to your computer and use it in GitHub Desktop.
Save Kcko/3b8cf9aed62ad6065d2f1692906e652b 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment