Skip to content

Commit

Permalink
upset info about attached()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 20, 2019
1 parent a2dd676 commit a3c177d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
33 changes: 5 additions & 28 deletions cs/components.texy
Expand Up @@ -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:

Expand All @@ -419,40 +419,17 @@ 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';
});
// ...
}

// ...
}
\--

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
Expand Down
33 changes: 5 additions & 28 deletions en/components.texy
Expand Up @@ -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:

Expand All @@ -416,40 +416,17 @@ 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';
});
// ...
}

// ...
}
\--

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
Expand Down

0 comments on commit a3c177d

Please sign in to comment.