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

Including the code twice #110

Open
AngelGris opened this issue Jan 11, 2018 · 3 comments
Open

Including the code twice #110

AngelGris opened this issue Jan 11, 2018 · 3 comments

Comments

@AngelGris
Copy link

The transformer works great, but I noticed it puts the JS code twice in the inclusion view. This doesn't prevent the transformer from working, but generates extra unnecessary code in the final HTML.
For instance, I created this in a Laravel Controller:

Javascript::put([
  'application' => [
      'name' => config('app.name', 'Laravel'),
  ],
  'user' => [
      'name' => $user->name,
  ]
]);

And this is what I get in the HTML:

<script>
    window.reactBinding = window.reactBinding || {};
    reactBinding.application = {"name":"AppName"};
    reactBinding.user = {"name":"Kemmer-Legros"};
</script>
<script>
    window.reactBinding = window.reactBinding || {};
    reactBinding.application = {"name":"AppName"};
    reactBinding.user = {"name":"Kemmer-Legros"};
</script>

Not big thing in my case, but if you are trying to send a big amount of information from Laravel to JS it could become an issue.

@codegain
Copy link

codegain commented Feb 5, 2018

I had the same issue, because my command renders a view inside a job which is queued in redis.
The queue doesn't restart between command calls and so the JavaScript::put() call keeps adding listeners to the event dispatcher for the same event again.

I fixed it by adding this code before my JavaScript::put() call:

$dispatcher = app()->make(Dispatcher::class);
$event = 'composing: ' . config('javascript.bind_js_vars_to_this_view');
if ($dispatcher->hasListeners($event)) {
        $dispatcher->forget($event);
}
\JavaScript::put([...

@kamilmodzelewski
Copy link

kamilmodzelewski commented Mar 22, 2018

code above does not work for me, something else is causing it, or event is registered in other way

@morksinaanab
Copy link

morksinaanab commented Dec 22, 2019

I have this problem as well, that queued tasks adding the JS code multiple times. Your fix got me in the right direction @codegain. However your fix checks if there has been any listener listening to add to the view, which means that if you add Javascript::put() for two different parts, the first one will be removed, and only the second one will be added (because each Javascript::put() adds a new listener).

What I've done is in the package code itself keep track of each js that is to be added with an array with hashes in the $view data. If there already is a hash for the js code, skip it. It seems todo the trick.

It's not really a solution to the problem that the queue is adding multiple (unnecessary) listeners, but it is a failsafe. I'm asking for a pull request now. #129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants