Last active
August 29, 2015 14:13
-
-
Save Kcko/17307354f0e79a28ec2d to your computer and use it in GitHub Desktop.
Nette - ajaxové hlasování
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{snippet} | |
<a n:href="update!" class="ajax">{$row->firstname} {$row->lastname}</a>: {$row->vote} <br /> | |
{/snippet} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
}); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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