Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ For Laravel users, there is a service provider you can make use of to automatica
];
```


When this provider is booted, you'll gain access to a helpful `JavaScript` facade, which you may use in your controllers.

```php
Expand Down Expand Up @@ -83,7 +82,7 @@ php artisan vendor:publish
php artisan vendor:publish --provider="Laracasts\Utilities\JavaScript\JavaScriptServiceProvider"
```

This will add a new configuration file to: `config/javascript.php`.
This will add a new configuration file to: `config/javascript.php`

```php
<?php
Expand Down Expand Up @@ -116,6 +115,12 @@ return [
];
```

and copy the view to `views/vendor/utilities/javascript.blade.php`

```php
<script>{{$js}}</script>
```

#### bind_js_vars_to_this_view

You need to update this file to specify which view you want your new JavaScript variables to be prepended to. Typically, your footer is a good place for this.
Expand All @@ -129,16 +134,19 @@ By default, all JavaScript vars will be nested under the global `window` object.
then you'll access all JavaScript variables, like so:

```js
MyNewNamespace.varName
MyNewNamespace.varName;
```

#### Note

Run this artisan command after changing the view path.

```
php artisan config:clear
```

### Symfony2

To use this component in Symfony2 applications you can try [this bundle](https://github.com/holyspecter/HospectPhpVarsToJsBundle), built on top of PHP-Vars-To-Js-Transformer.

### Without Laravel
Expand Down
5 changes: 4 additions & 1 deletion src/JavaScriptServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public function register()
public function boot()
{
$this->publishes([
__DIR__ . '/config/javascript.php' => config_path('javascript.php')
__DIR__ . '/config/javascript.php' => config_path('javascript.php'),
__DIR__ . '/views' => base_path('resources/views/vendor/utilities')
]);

$this->loadViewsFrom(__DIR__ . '/views', 'utilities');

if (class_exists('Illuminate\Foundation\AliasLoader')) {
AliasLoader::getInstance()->alias(
'JavaScript',
Expand Down
5 changes: 4 additions & 1 deletion src/LaravelViewBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laracasts\Utilities\JavaScript;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\View\View;

class LaravelViewBinder implements ViewBinder
{
Expand Down Expand Up @@ -39,9 +40,11 @@ public function __construct(Dispatcher $event, $views)
*/
public function bind($js)
{

foreach ($this->views as $view) {
$this->event->listen("composing: {$view}", function () use ($js) {
echo "<script>{$js}</script>";
$utilities = view("utilities::javascript", ['js' => $js]);
echo $utilities->render();
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/views/javascript.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>{!! $js !!}</script>