ComponentModel – order of attaching/loading

Notice: This thread is very old.
juzna.cz
Member | 248
+
0
-

When you have a hierarchy of components, in what order do you expect them to be attached (method attach gets called) and restored (method loadState gets called)? Should it be parent-first or child-first?

I got confused now while writing components, which depend on each other. I (wrongly) assumed that parent is initialized first, so that I can read it's state within sub-component. Current implementation behaves in child-first manner.

Which way is “correct” and why? Is is correct atm? Thanks

HosipLan
Moderator | 4668
+
0
-

Just to add sort of test case

class Component extends Nette\ComponentModel\Container
{
        public function __construct()
        {
                $this->monitor('Presenter');
        }

        protected function attached($obj)
        {
                parent::attached($obj);
                echo get_class($this), ' ', $this->name, ' to ', $obj->name, "<br>\n";
        }
}

class Presenter extends Component { }

$three = new Presenter;
$three->setParent(NULL, 'parent');

$two = new Component;
$two->addComponent(new Component, 'one');

$three->addComponent($two, 'two');

results into

Component one to parent
Component two to parent