Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only add js (checked by hash) once #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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