Po kliknutí na první handle:
Handle z třídy column, ID: 1
Handle z list component
Handle z presenteru
Handle z anonymní funkce
| <?php | |
| class Column extends \Nette\Object | |
| { | |
| /** | |
| * @var string | |
| */ | |
| public $name; | |
| /** | |
| * @var array | |
| */ | |
| public $onClick; | |
| /** | |
| * @param $name | |
| */ | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| $this->onClick[] = function(ListComponent $lc, array $params) { | |
| echo("<p>Handle z třídy column, ID: ".$params['id']."</p>\n"); | |
| }; | |
| } | |
| /** | |
| * @param ListComponent $lc | |
| * @param array $params | |
| */ | |
| public function Click(ListComponent $lc, $params) | |
| { | |
| $this->onClick($lc, $params); | |
| } | |
| } |
| {block content} | |
| {* default config *} | |
| {control list} | |
| {* config in presenter *} | |
| {control list2} |
| <?php | |
| /** | |
| * Homepage presenter. | |
| */ | |
| class HomepagePresenter extends BasePresenter | |
| { | |
| /** | |
| * @param ListComponent $lc | |
| * @param array $params | |
| */ | |
| public function handlePresenter(ListComponent $lc, array $params) | |
| { | |
| echo("<p>Handle z presenteru</p>\n"); | |
| } | |
| /** | |
| * @return ListComponent | |
| */ | |
| protected function createComponentList() | |
| { | |
| return new ListComponent($this, 'list'); | |
| } | |
| /** | |
| * Alternative configuration replace default | |
| * @return ListComponent | |
| */ | |
| protected function createComponentList2() | |
| { | |
| $list = new ListComponent($this, 'list2'); | |
| $cols['jedna'] = new Column('jedna'); | |
| $cols['jedna']->onClick[] = $list->handleGeneric; | |
| $cols['jedna']->onClick[] = $this->handlePresenter; | |
| $cols['jedna']->onClick[] = function(ListComponent $lc, array $params) { | |
| echo("<p>Handle z anonymní funkce</p>\n"); | |
| die(); | |
| }; | |
| $cols['dva'] = new Column('dva'); | |
| $list->cols = $cols; | |
| return $list; | |
| } | |
| } |
| <p> | |
| {foreach $cols as $c} | |
| {$c->name}<a n:href="default! $c->name, $iterator->counter">link s id {$iterator->counter}</a> | |
| {/foreach} | |
| </p> |
| <?php | |
| class ListComponent extends \Nette\Application\UI\Control | |
| { | |
| /** | |
| * @var array|column[] | |
| */ | |
| public $cols; | |
| /** | |
| * @param Nette\ComponentModel\IContainer $parent | |
| * @param null $name | |
| */ | |
| public function __construct($parent, $name) | |
| { | |
| parent::__construct($parent, $name); | |
| $this->config(); | |
| } | |
| /** | |
| * Configure handles | |
| */ | |
| public function config() | |
| { | |
| $this->cols['jedna'] = new Column('jedna'); | |
| $this->cols['jedna']->onClick[] = $this->handleGeneric; | |
| $this->cols['jedna']->onClick[] = $this->presenter->handlePresenter; | |
| $this->cols['jedna']->onClick[] = function(ListComponent $lc, array $params) { | |
| echo("<p>Handle z anonymní funkce</p>\n"); | |
| die(); | |
| }; | |
| $this->cols['dva'] = new Column('dva'); | |
| } | |
| /** | |
| * Render | |
| */ | |
| public function render() | |
| { | |
| $this->template->cols = $this->cols; | |
| $this->template->setFile(__DIR__ . '/ListComponent.latte'); | |
| $this->template->render(); | |
| } | |
| /** | |
| * @param string $name | |
| * @param int $id | |
| */ | |
| public function handleDefault($name, $id) | |
| { | |
| $col = $this->cols[$name]; | |
| $params = array('id' => $id); | |
| $col->Click($this, $params); | |
| } | |
| /** | |
| * @param ListComponent $lc | |
| * @param array $params | |
| */ | |
| public function handleGeneric(ListComponent $lc, array $params) | |
| { | |
| echo("<p>Handle z list component</p>\n"); | |
| } | |
| } |