Skip to content

Commit f1ee593

Browse files
authored
Create LazyComponent.php
1 parent cd2bb26 commit f1ee593

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\LazyComponent;
6+
7+
abstract class LazyComponent extends \Nette\Application\UI\Control
8+
{
9+
private bool $loaded = false;
10+
11+
public function handleLoadComponent() : void
12+
{
13+
$this->loaded = true;
14+
$this->redrawControl();
15+
}
16+
17+
public function renderLoadComponentLink() : void
18+
{
19+
echo $this->link('loadComponent');
20+
}
21+
22+
public function render() : void
23+
{
24+
if (!$this->loaded) {
25+
$this->template->setFile(__DIR__ . '/emptyComponent.latte');
26+
} else {
27+
$this->beforeRender();
28+
$this->template->setFile(\str_replace('.php', '.latte', static::getReflection()->getFileName()));
29+
}
30+
31+
$this->template->render();
32+
}
33+
34+
public function saveState(array &$params): void
35+
{
36+
parent::saveState($params);
37+
$params['lazyComponent_loaded'] = $this->loaded;
38+
}
39+
40+
public function loadState(array $params): void
41+
{
42+
parent::saveState($params);
43+
$this->loaded = (bool) ($params['lazyComponent_loaded'] ?? $this->loaded);
44+
}
45+
46+
protected function beforeRender() : void
47+
{
48+
}
49+
}

0 commit comments

Comments
 (0)