diff --git a/cs/components.texy b/cs/components.texy index f95c4f93c6..d0f9533db5 100644 --- a/cs/components.texy +++ b/cs/components.texy @@ -410,7 +410,7 @@ $parent->addComponent($control, 'shortNews'); Monitorování předků ------------------- -Jak poznat, kdy byla komponenta připojena do stromu presenteru? Sledovat změnu rodiče nestačí, protože k presenteru mohl být připojen třeba rodič rodiče. Pomůže metoda [monitor($type)|api:Nette\ComponentModel\Component::monitor()]. Každá komponenta může monitorovat libovolný počet tříd a rozhraní. Připojení nebo odpojení je ohlášeno zavoláním metody `attached($obj)` resp. `detached($obj)`, kde `$obj` je objekt sledované třídy. +Jak poznat, kdy byla komponenta připojena do stromu presenteru? Sledovat změnu rodiče nestačí, protože k presenteru mohl být připojen třeba rodič rodiče. Pomůže metoda [monitor($type, $attached, $detached)|api:Nette\ComponentModel\Component::monitor()]. Každá komponenta může monitorovat libovolný počet tříd a rozhraní. Připojení nebo odpojení je ohlášeno zavoláním callbacku `$attached` resp. `$detached`, a předáním objektu sledované třídy. Pro lepší pochopení příklad: třída `UploadControl`, reprezentující formulářový prvek pro upload souborů v Nette Forms, musí formuláři nastavit atribut `enctype` na hodnotu `multipart/form-data`. V době vytvoření objektu ale k žádnému formuláři připojena být nemusí. Ve kterém okamžiku tedy formulář modifikovat? Řešení je jednoduché - v konstruktoru se požádá o monitoring: @@ -419,7 +419,9 @@ class UploadControl extends Nette\Forms\Controls\BaseControl { public function __construct($label) { - $this->monitor(Nette\Forms\Form::class); + $this->monitor(Nette\Forms\Form::class, function ($form) { + $form->getElementPrototype()->enctype = 'multipart/form-data'; + }); // ... } @@ -427,32 +429,7 @@ class UploadControl extends Nette\Forms\Controls\BaseControl } \-- -a jakmile je formulář k dispozici, zavolá se metoda attached: - -/--php -protected function attached($form) -{ - parent::attached($form); - - if ($form instanceof Nette\Forms\Form) { - $form->getElementPrototype()->enctype = 'multipart/form-data'; - } -} -\-- - -Od `nette/component-model` v2.4 je preferovaný způsob předat callbacky přímo metodě `monitor($type, $attached = null, $detached = null)`: - -/--php -class UploadControl extends Nette\Forms\Controls\BaseControl -{ - public function __construct($label) - { - $this->monitor(Nette\Forms\Form::class, function ($form) { - $form->getElementPrototype()->enctype = 'multipart/form-data'; - }); - } -} -\-- +a jakmile je formulář k dispozici, zavolá se callback. (Dříve se místo něj používala společná metoda `attached` resp. `detached`). Iterování nad dětmi diff --git a/en/components.texy b/en/components.texy index 45a34e1671..e35117f4a2 100644 --- a/en/components.texy +++ b/en/components.texy @@ -407,7 +407,7 @@ $parent->addComponent($control, 'shortNews'); Monitoring of Ancestors ----------------------- -How to find out when was component added to presenter's tree? Watching the change of parent is not enough because a parent of parent might have been added to presenter. Method [monitor($type)|api:Nette\ComponentModel\Component::monitor()] is here to help. Every component can monitor any number of classes and interfaces. Adding or removing is reported by invoking method `attached($obj)` (`detached($obj)` respectivelly), where `$obj` is the object of monitored class. +How to find out when was component added to presenter's tree? Watching the change of parent is not enough because a parent of parent might have been added to presenter. Method [monitor($type, $attached, $detached)|api:Nette\ComponentModel\Component::monitor()] is here to help. Every component can monitor any number of classes and interfaces. Adding or removing is reported by invoking callback `$attached` (`$detached` respectivelly), where the object of monitored class is passed. An example: Class `UploadControl`, representing form element for uploading files in Nette Forms, has to set form's attribute `enctype` to value `multipart/form-data`. But in the time of the creation of the object it does not have to be attached to any form. When to modify the form? The solution is simple - we create a request for monitoring in the constructor: @@ -416,7 +416,9 @@ class UploadControl extends Nette\Forms\Controls\BaseControl { public function __construct($label) { - $this->monitor(Nette\Forms\Form::class); + $this->monitor(Nette\Forms\Form::class, function ($form) { + $form->getElementPrototype()->enctype = 'multipart/form-data'; + }); // ... } @@ -424,32 +426,7 @@ class UploadControl extends Nette\Forms\Controls\BaseControl } \-- -and method `attached` is called when the form is available: - -/--php -protected function attached($form) -{ - parent::attached($form); - - if ($form instanceof Nette\Forms\Form) { - $form->getElementPrototype()->enctype = 'multipart/form-data'; - } -} -\-- - -Since `nette/component-model` v2.4, the preferred way is to pass the callbacks directly to `monitor($type, $attached = null, $detached = null)`: - -/--php -class UploadControl extends Nette\Forms\Controls\BaseControl -{ - public function __construct($label) - { - $this->monitor(Nette\Forms\Form::class, function ($form) { - $form->getElementPrototype()->enctype = 'multipart/form-data'; - }); - } -} -\-- +and when the form is available callback is called. (Formely methods `attached` or `detached` was used instead). Iterating over Children