Independent Components 
“Can we build an app only from components?” 
Adam Štipák | @new_POPE | adam.stipak@gmail.com
Problem? 
Using Presenter in Component.
Problem? 
Using Presenter in Component. 
● no point because Presenter is PresenterComponent. 
● what is the benefit? 
● dont use Presenter but use Events.
Wish. 
I want build app “only” from Components
Wish. 
I want build app “only” from Components 
without dependency to Presenters
No problem! Just use Events. 
Use events 
or KdybyEvents 
class BarPresenter { 
protected function createFoo() { 
$control = new FooControl; 
$control->onSomething[] = 
function(FooControl $foo, … ) { 
$this->redirect(‘Bar:’); 
}; 
} 
} 
Best practice tip: http://pla.nette.org/cs/best-practise-formulare-jako-komponenty#toc-ui-control
Solved.
But no! 
The dependency is still there.
Dependency is here. 
If you are using 
{plink Presenter:action} 
or 
NextrasLinkFactory 
->link(‘Presenter:’); 
the devil is still here. 
FooPresenter 
FooComponent 
{plink Bar:} 
BarPresenter
We can wrap it. 
class MyRequest 
extends NetteApplicationRequest { 
function __construct( … ) { 
parent::__construct( … ); 
$this->name = “Presenter”; 
//action is in the parameters now :( 
$this->action = “action”; 
} 
} 
// in FooComponent only setup request 
LinkFactory::link( 
MyRequest::setParameters( … ) 
); 
FooPresenter 
FooComponent 
MyRequest BarPresenter 
parameters
Components are isolated. 
● are isolated 
● requests are dependencies 
● has own “universe” 
○ can create links but only to itself! 
● still using events for everything other 
○ like redirect after some action (form save and so on)
Presenters managing redirects. 
● managing redirects 
● creating components 
● composing views from components
Pros 
● better component isolation 
● better testability 
● better styling (preprocessors) 
● component factory
Cons 
● more writing 
● not fit for CLI apps (use KdybyConsole) 
● component factory (?do=foo-bar)
How to write 
APP?
Everything will be a Component 
Bar.default.latte 
{control mainMenu} 
{control langSelector} 
{control login} 
{control mainMenu:small} 
Bar.default.latte 
Home | Project | About | Contact 
EN | SK | CZ 
Email Password 
Log me in 
home | project | about | contact
2 types of Component 
● Static component 
○ templates/components/… 
● Dynamic component 
○ instance of NetteApplicationUIControl 
○ has own Service for: 
■ data storage 
■ data manipulation 
■ listening Events (KdybyEvents) 
■ …
Summary 
● use events for “in app” communication 
● use Requests 
○ if Component needs to “call” another part of the app 
● “Component first access” 
○ build app only from Components 
● keep Presenters clean 
○ only setup components for redirects
Thanks 
Thanks to SMACSS and AngularJS for the idea 
Adam Štipák | @new_POPE | adam.stipak@gmail.com | 2014

Independent components in Nette Framework

  • 1.
    Independent Components “Canwe build an app only from components?” Adam Štipák | @new_POPE | adam.stipak@gmail.com
  • 2.
  • 3.
    Problem? Using Presenterin Component. ● no point because Presenter is PresenterComponent. ● what is the benefit? ● dont use Presenter but use Events.
  • 4.
    Wish. I wantbuild app “only” from Components
  • 5.
    Wish. I wantbuild app “only” from Components without dependency to Presenters
  • 6.
    No problem! Justuse Events. Use events or KdybyEvents class BarPresenter { protected function createFoo() { $control = new FooControl; $control->onSomething[] = function(FooControl $foo, … ) { $this->redirect(‘Bar:’); }; } } Best practice tip: http://pla.nette.org/cs/best-practise-formulare-jako-komponenty#toc-ui-control
  • 7.
  • 8.
    But no! Thedependency is still there.
  • 9.
    Dependency is here. If you are using {plink Presenter:action} or NextrasLinkFactory ->link(‘Presenter:’); the devil is still here. FooPresenter FooComponent {plink Bar:} BarPresenter
  • 10.
    We can wrapit. class MyRequest extends NetteApplicationRequest { function __construct( … ) { parent::__construct( … ); $this->name = “Presenter”; //action is in the parameters now :( $this->action = “action”; } } // in FooComponent only setup request LinkFactory::link( MyRequest::setParameters( … ) ); FooPresenter FooComponent MyRequest BarPresenter parameters
  • 11.
    Components are isolated. ● are isolated ● requests are dependencies ● has own “universe” ○ can create links but only to itself! ● still using events for everything other ○ like redirect after some action (form save and so on)
  • 12.
    Presenters managing redirects. ● managing redirects ● creating components ● composing views from components
  • 13.
    Pros ● bettercomponent isolation ● better testability ● better styling (preprocessors) ● component factory
  • 14.
    Cons ● morewriting ● not fit for CLI apps (use KdybyConsole) ● component factory (?do=foo-bar)
  • 15.
  • 16.
    Everything will bea Component Bar.default.latte {control mainMenu} {control langSelector} {control login} {control mainMenu:small} Bar.default.latte Home | Project | About | Contact EN | SK | CZ Email Password Log me in home | project | about | contact
  • 17.
    2 types ofComponent ● Static component ○ templates/components/… ● Dynamic component ○ instance of NetteApplicationUIControl ○ has own Service for: ■ data storage ■ data manipulation ■ listening Events (KdybyEvents) ■ …
  • 18.
    Summary ● useevents for “in app” communication ● use Requests ○ if Component needs to “call” another part of the app ● “Component first access” ○ build app only from Components ● keep Presenters clean ○ only setup components for redirects
  • 19.
    Thanks Thanks toSMACSS and AngularJS for the idea Adam Štipák | @new_POPE | adam.stipak@gmail.com | 2014