Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active August 29, 2015 14:13
Show Gist options
  • Save Kcko/17307354f0e79a28ec2d to your computer and use it in GitHub Desktop.
Save Kcko/17307354f0e79a28ec2d to your computer and use it in GitHub Desktop.
Nette - ajaxové hlasování
<?
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();
}
}
}
{snippet}
<a n:href="update!" class="ajax">{$row->firstname} {$row->lastname}</a>: {$row->vote} <br />
{/snippet}
<?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);
});
}
{foreach $rows as $row}
{control vote-$row->id}
{/foreach}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment