[2011–10–07] added Nette\Application\UI\Multiplier
- David Grudl
- Nette Core | 8229
The Nette\Application\UI\Multiplier is extremely simple class which allows you dynamically create components.
This is an example of common component factory for single component:
{control poll}
presenter:
class SomePresenter extends Nette\Application\UI\Presenter
{
protected function createComponentPoll()
{
$poll = new PollControl($this->pollModel);
$poll->onVote[] = callback($this, 'voted');
return $poll;
}
}
And what about multiple polls on page? Template:
{control poll-1}
<hr>
{control poll-2}
<hr>
{control poll-three}
{foreach [1,2,3] as $id} {control "poll-$id"} {/foreach}
presenter:
class AnyPresenter extends Nette\Application\UI\Presenter
{
protected function createComponentPoll()
{
$presenter = $this;
return new Nette\Application\UI\Multiplier(function($id) use ($presenter) {
$poll = new PollControl($presenter->pollModels[$id]);
$poll->onVote[] = callback($presenter, 'voted');
return $poll;
});
}
}
The name poll-1
is understood as poll
component
(created by createComponentPoll
) and 1
subcomponent
(created by closure).
- David Grudl
- Nette Core | 8229
Nox wrote:
That's great news! imho this feature was really missing in Nette
I think this is not feature, this is design pattern ;-)
paranoiq wrote:
Will Nette get a new branch for PHP 5.4?
Is there any code that is written in PHP 5.4 different?
- grey
- Member | 94
David Grudl wrote:
Nox wrote:
That's great news! imho this feature was really missing in Nette
I think this is not feature, this is design pattern ;-)
paranoiq wrote:
Will Nette get a new branch for PHP 5.4?
Is there any code that is written in PHP 5.4 different?
In Nette\Http\RequestFactory, it fails on checking magic_quotes, because they don't exist in PHP 5.4.
- grey
- Member | 94
David Grudl wrote:
Fails? Are you sure? Did you try it?
http://scr.over.cz/1318281529.png
Well, it is just deprecated, but it would be nice to have version working in
strict mode, or whatever stops the script from executing.
- Vojtěch Dobeš
- Gold Partner | 1316
I've run into “snippet crisis” using the Multiplier
. I have
standard FooControl
, that invalidates itself on signal.
public function handleBar()
{
$this->invalidateControl();
}
But it doesn't send back its snippet in payload. After searching in source
code, I figured out, that it might be caused by
source-Latte\Macros\UIMacros.php::501 – Multiplier
isn't
Control
, thus it is not checked by isControlInvalid()
.
I fixed it by few lines of code
but is there some more elegant solution (some snippet envelope maybe)?
Last edited by vojtech.dobes (2011-10-19 12:50)
- tomask
- Member | 9
I am trying dynamic snippets the same way as Vojta Dobes did (http://www.youtube.com/watch?…), but I have the same problem, payload does't send invalidated snippets. Payload contains only state information.
Component works (=send snippets in payload) as the static component but doesn' t when dynamic with Multiplier.
I am using Nette stable 2.0.1 from 29.2.2012.
Do you have any ideas, why it doesn't work?
TestPresenter
*************
protected function createComponentMyComponent()
{
$that = $this;
return new \Nette\Application\UI\Multiplier(function($name, $parent) use ($that) {
$control = new \TestControl\Test;
return $control;
});
Presenter template default.phtml
********************************
{block content}
{foreach array(1,2,3) as $step}
{control myComponent-$step}
{/foreach}
{/block}
Control
*******
use Nette\Application\UI\Control,
Nette\Diagnostics\Debugger;
class Test extends Control
{
public function handleNeco()
{
$this->invalidateControl();
}
public function render()
{
$this->template->setFile(dirname(__FILE__) . '/Testovaci.phtml')->render();
}
}
Control template Testovaci.phtml
********************************
{snippet prvni}
<a n:href="neco!" class="ajax">Handle!</a>
({time()})<br />
{/snippet}
{snippet druhy}
({time()})
{/snippet}
- Vojtěch Dobeš
- Gold Partner | 1316
I caused some serious mystification during my presentation :) Later I realized I was showing the example with Nette including bugfix by Honza Tvrdík, not the actual nightly build at that moment. There is already fix for this bug on the master branch, but it didn't make it to v2.0.1.
The fix is contained in this commit: https://github.com/…2ad320473551