Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/LaravelViewBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ public function __construct(Dispatcher $event, $views)

/**
* Bind the given JavaScript to the view.
* Tracks if Javascript has already been added
* by keeping hashes of the js code to check against in the view data
*
* @param string $js
*/
public function bind($js)
{
foreach ($this->views as $view) {
$this->event->listen("composing: {$view}", function () use ($js) {
echo "<script>{$js}</script>";
$this->event->listen("composing: {$view}", function ($view) use ($js) {
$data = $view->getData();
$hashes = isset($data['hashes']) ? $data['hashes'] : [];
$hash = md5($js);
if (!in_array($hash,$hashes)) {
$hashes[] = md5($js);
$view->with('hashes',$hashes);
echo "<script>{$js}</script>";
}
});
}
}
Expand Down