Skip to content

Loading…

Multiplier breaks AJAX in included Controls #390

Closed
vojtech-dobes opened this Issue · 10 comments

4 participants

@vojtech-dobes
Nette Foundation member

It is caused by implementation of method renderSnippets, specifically line 501. Components attached via Multiplier are not searched at all, and because of that they cannot be rendered.
I fixed it in my application by direct modification of the renderSnippets method - is this suitable solution for pull request?

<?php
$httpParams = $control->getPresenter()->getContext()->httpRequest->getQuery();
foreach ($control->getComponents(FALSE, 'Nette\Application\UI\Multiplier') as $multiplier) {
    foreach ($httpParams as $param => $signal) {
        if ($param == 'do' && Strings::startsWith($signal, $multiplier->getName()) && ($pos = strrpos($signal, '-')) !== FALSE) {
            $child = $control->getComponent(substr($signal, 0, $pos));
            $child->snippetMode = TRUE;
            $child->render();
            $child->snippetMode = FALSE;
        }
    }
}
# https://gist.github.com/1297934

Topic on forum: http://forum.nette.org/en/929-2011-10-07-added-nette-application-ui-multiplier#p4012

@dg dg closed this
@vojtech-dobes
Nette Foundation member

Just a question - does this close mean "send that stuff as pull request" :) ? I mean it does work in my case... if I see correctly, it hasn't been fixed somewhere else.

@dg
Nette Foundation member
dg commented

I hope it was fixed here 3aefa50 - isn't it?

@dg dg reopened this
@vojtech-dobes
Nette Foundation member

Didn' t try it (I will), but expect it wasn't. It's more like #93. But I will check it, I am not sure :)

@dg
Nette Foundation member
dg commented

Oh, it is not the same. Reopened.

@vojtech-dobes
Nette Foundation member

No, it is still not fixed. Because renderSnippets does not work with Controls in Multiplier at all. The hack above is pretty ugly, but tries to find all activated controls attached through Multiplier.

@JanTvrdik
Nette Foundation member

Experimental fix, written on Nette Brain Cloud.

public static function renderSnippets($control, $local, $params)
{
    $control->snippetMode = FALSE;
    $payload = $control->getPresenter()->getPayload();
    if (isset($local->blocks)) {
        foreach ($local->blocks as $name => $function) {
            if ($name[0] !== '_' || !$control->isControlInvalid(substr($name, 1))) {
                continue;
            }
            ob_start();
            $function = reset($function);
            $snippets = $function($local, $params);
            $payload->snippets[$id = $control->getSnippetId(substr($name, 1))] = ob_get_clean();
            if ($snippets) {
                $payload->snippets += $snippets;
                unset($payload->snippets[$id]);
            }
        }
    }

    if ($control instanceof Nette\Application\UI\IRenderable) {
        $queue = array($control);
        do {
            foreach (array_shift($queue)->getComponents() as $child) {
                if ($child instanceof Nette\Application\UI\IRenderable) {
                    if ($child->isControlInvalid()) {
                        $child->snippetMode = TRUE;
                        $child->render();
                        $child->snippetMode = FALSE;
                    }

                } elseif ($child instanceof Nette\ComponentModel\IContainer) {
                    $queue[] = $child;
                }
            }

        } while ($queue);
    }
}
@hrach

Trochu sem se teď lekl toho 2.1. Toto musí být ve 2.0 ;)
PS: Honzo, udělej pull request, nebo ho udělám já a budu mít tam jméno svoje :P

@JanTvrdik
Nette Foundation member

I'm not gonna create a pull request from code which I don't fully understand. Someone who understands how snippets are currently implemented must have a look at this, write unit test and verify that it really resolves the problem and does not create any new.

@hrach

That's not true. The fix is just as simple as this issue is and solves it correctly. There is no doubt about compulsory tests. However, you are talking about complex solution of the snippet-ajax-problem which nobody claimed to be solved by that of.

@hrach

close please.

@dg dg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.