Kcko
1/17/2015 - 7:19 PM

Nette - ajaxové hlasování

Nette - ajaxové hlasování

				
				{foreach $rows as $row}
					{control vote-$row->id}
				{/foreach}
  <?php
    protected function createComponentVote()
	{
	    $rows = $this->database->table('user');
	    
	    $rows_ = array();
	    foreach ($rows as $row)
	    {
	    	$rows_[$row->id] = $row;
	    }

	    return new Nette\Application\UI\Multiplier(function ($userId) use ($rows_) {
	        return new App\FrontModule\Components\Vote($rows_[$userId], $this->database);
	    });
	}
	
{snippet}
	<a n:href="update!" class="ajax">{$row->firstname} {$row->lastname}</a>: {$row->vote} <br />
{/snippet}
<?

namespace App\FrontModule\Components;

use Nette\Application\UI,
	Nette,
	App,
	Cardbook;

class Vote extends UI\Control 
{

	private $user;
	private $database;


	public function __construct ($user, Nette\Database\Context $database)
	{
		$this->user = $user;
		$this->database = $database;
		parent::__construct();
	}


	public function render() 
	{
		$this->template->setFile(__DIR__ . '/../templates/components/Vote/test.latte');
		$this->template->row = $this->user;
		$this->template->render();
	}	




	public function handleUpdate()
	{

		$userId = $this->user->id;
		
		$user = $this->database->table('user')->where('id', $userId)->fetch();
		$vote = $user->vote + 1;
		$user->update(array("vote" => $vote));

		if ($this->presenter->isAjax())
		{
			$this->redrawControl();
		}
	}


}