[2011–10–07] added Nette\Application\UI\Multiplier

Notice: This thread is very old.
David Grudl
Nette Core | 7418
+
0
-

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).

Nox
Member | 378
+
0
-

That's great news! imho this feature was really missing in Nette

redhead
Member | 1313
+
0
-

I second that. Nice!

paranoiq
Member | 390
+
0
-

btw: this can be done without the $presenter = $this; PHP 5.4. will Nette get a new branch for PHP 5.4?

grey
Member | 94
+
0
-

paranoiq wrote:

btw: this can be done without the $presenter = $this; PHP 5.4. will Nette get a new branch for PHP 5.4?

PHP 5.4 branch would be great, even if only for magic_quotes issues…

David Grudl
Nette Core | 7418
+
0
-

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
+
0
-

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.

David Grudl
Nette Core | 7418
+
0
-

Fails? Are you sure? Did you try it?

grey
Member | 94
+
0
-

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.

David Grudl
Nette Core | 7418
+
0
-

This is already fixed.

Vojtěch Dobeš
Gold Partner | 1316
+
0
-

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)

Jan Tvrdík
Nette guru | 2565
+
0
-

Probably related issue.

Last edited by Jan Tvrdík (2011-10-19 19:19)

bazo
Member | 619
+
0
-

is there any plan to fix this directly in Nette in the near future?

Sajmi
Member | 9
+
0
-

How does it look like with this issue? Ajax can't be used within multiplied controls…

mcmatak
Member | 485
+
0
-

does it mean that if you use multiplier you have to always use the sufix?

firstly you were using {control poll}

now you used multiplier, so from now, even if you dont want to use poll multiple, you have to use suffix?

poll-default?

no other options?

HosipLan
Moderator | 4668
+
0
-

That's just the way the components works.

tomask
Member | 9
+
0
-

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
+
0
-

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