Search code examples
javascriptphpajaxnette

Nette - snippet update


I have a problem with snippet updating. After changing the selection in the select box, I redraw the contents of options in another select box but the snippet doesn't update.

Latte:

<form class="df-checkout">
...

<select n:href="getHraci!" name="domaci" id="domaci" class="form-control">
    <option value="">Vybrat</option>
    <option n:foreach="$tymy as $tym" value="{$tym->getId()}">
        {$tym->getNazev()}
    </option>
</select>

...

<div class="row helpers hidden">
    <select n:snippet="hraciDomaci" class="form-goly-domaci-select form-control">
        <option></option>
        <option n:foreach="$hraciDomaci as $hrac" value="{$hrac->getId()}">
            {$hrac->getPrijmeni()} {$hrac->getJmeno()}
        </option>
    </select>

    <input type="text" class="form-goly-input form-control">
</div>

JS file:

$(document).ready(function(){
    $("#domaci").bind('change', function() {
        var link = $(this).attr("href");

        $.nette.ajax ({
            url: link,
            data: {"strana": "domaci", "tymId": $(this).val()},
            type: 'get',
            dataType:'json'
        });
    });
});

Controller:

public function handleGetHraci($strana, $tymId)
{
    $tym = $this->tymManager->getTymRepository()->find($tymId);
    $muzstvo = $this->tymManager->getMuzstvoRepository()->findBy(["nazev" => self::HLAVNI_TYM]);
    $hraci = $this->hracManager->getHracRepository()
                ->findBy(["tym" => $tym, "muzstvo" => $muzstvo], ["prijmeni" => "ASC", "jmeno" => "ASC"]);

    if($this->isAjax()){
        $this->template->hraciDomaci = $hraci;

        $this->redrawControl('hraciDomaci');
    }
}

The form has not yet been created and processed, so the first select box I have done is temporarily and select box with the snippet is independent on the form. I use it for copying. JS calls the handler correctly, and if I dump $this->template->hraciDomaci before redrawControl the data is there, but redrawControl will not do anything. But a new line with the process is added to the page in the lower Tracy bar. I do not have a bug in my debugger, the process has status 200 but response contains only:

{"state":[],"snippets":{"snippet--hraciDomaci":"\t\t\t\t\t\t\t\t\t\t<option></option>\n"}}

I tried to use $.get instead of $.nette.ajax, wrap in snippetArea and I normally have this code in {block content}, so snippetArea should not be needed. nette.ajax.js with initialization $.nette.init(); I have too.

Thanks a lot for any advice.


Solution

  • check that you are not rewriting hraciDomaci variable in render* method (e.g. renderDefault) in the presenter