Presenter variable unchanged by Ajax

Notice: This thread is very old.
richard
Member | 60
+
0
-

I am trying to add one to a presenter variable.

<?php
class TestPresenter extends BasePresenter
{

	/**
	 * Sign in form component factory.
	 * @return Nette\Application\UI\Form
	 */

	protected $testValue = 0;

	public function startup()
	{
	  parent::startup();
	}

    	public function renderDefault()
	{
	    $this->template->testValue = $this->testValue;
	}

	public function handleAddCount() {
	    $this->testValue++;
	    $this->invalidateControl('testSnippet');
	}
}
?>

and the template:

<?php
{block content}
    <a href="{link addCount!}" class="ajax">Add Count</a>
    {snippet testSnippet}
	{$testValue}
    {/snippet}
{/block}
?>

The $testValue won't change as expected. It is always 0 and handleAddCount() changes it to 1 only. I was studying the coffee machine tutorial for 0.9 and something similar worked there OK.

xxxObiWan
Member | 822
+
0
-

Shouldn`t the $testValue be persistent ?

/** @persistent */
protected $testValue = 0;

Last edited by xxxObiWan (2011-08-23 18:16)

richard
Member | 60
+
0
-

Yeah, /** @persistent */ works but only without ajax class on the link:

This adds one to the counter but is not ajax:

<?php
<a href="{link addCount!}">Add Count</a>
?>

This does the ajax spin but always prints 1, meaning the variable is not persistent…?

<?php
<a href="{link addCount!}" class="ajax">Add Count</a>
?>

Last edited by richard (2011-08-23 19:12)

na1k
Member | 288
+
0
-

How about

<?php
public function handleAddCount() {
   $this->testValue++;

   if ($this->isAjax()) {
      $this->invalidateControl('testSnippet');
   } else {
      $this->redirect('this');
   }
}
?>

(I have no idea why your code doesn't work, but I came around the same behavior once and the code above helped)

Patrik Votoček
Member | 2221
+
0
-

Do you have ‘testSnippet’ in payload response? (May need invalidate some parent snippet(s))

richard
Member | 60
+
0
-

My fault, like usually. The link with action needs to be wrapped by snippet and invalidated…
Thanks for yor response.