- Nette sandbox: https://github.com/nette/sandbox
- Nette ajax: https://github.com/vojtech-dobes/nette.ajax.js
nette.ajax.js
aspinner.ajax.js
přidáme do šablony@layout.latte
- Nahradíme soubory HomepagePresenteru
- Někam (např.
app/components
) umístíme komponentu
Last active
March 14, 2023 00:04
-
-
Save Kedrigern/4672895 to your computer and use it in GitHub Desktop.
Nette: Ajaxové ovládání komponenty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{* 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{* 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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)