-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAsyncControlTrait.php
More file actions
66 lines (54 loc) · 1.31 KB
/
AsyncControlTrait.php
File metadata and controls
66 lines (54 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php declare(strict_types = 1);
namespace Pd\AsyncControl\UI;
use Nette\Application\UI\Control;
use Nette\Bridges\ApplicationLatte\Template;
/**
* @method render()
*/
trait AsyncControlTrait
{
/**
* @var callable
*/
protected $asyncRenderer;
public function handleAsyncLoad(): void
{
if ( ! $this instanceof Control || ! ($presenter = $this->getPresenter(FALSE)) || ! $presenter->isAjax()) {
return;
}
ob_start(function () {
});
try {
$this->doRender();
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
}
$content = ob_get_clean();
$presenter->getPayload()->snippets[$this->getSnippetId('async')] = $content;
$presenter->sendPayload();
}
/**
* @param array<string, string> $linkAttributes
* @return void
*/
public function renderAsync(string $linkMessage = NULL, array $linkAttributes = NULL): void
{
$template = $this->createTemplate();
$template->link = new AsyncControlLink($linkMessage, $linkAttributes);
$template->setFile(__DIR__ . '/templates/asyncLoadLink.latte');
$template->render();
}
public function setAsyncRenderer(callable $renderer): void
{
$this->asyncRenderer = $renderer;
}
protected function doRender(): void
{
if (is_callable($this->asyncRenderer)) {
call_user_func($this->asyncRenderer);
} else {
$this->render();
}
}
}