parallax / laravel-js-views

1 stars 0 forks source link

Globals method #20

Closed bradlc closed 6 years ago

bradlc commented 6 years ago

You can currently add globals in the config file, but this doesn’t work for dynamic values as the config is cached by Laravel.

We should add a method which can be used to define global JS variables at runtime:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Parallax\LaravelJsViews\Globals;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // single
        Globals::set('userId', \Auth::id());
        // multiple
        Globals::set(['aNumber' => 42, 'anotherNumber' => 123]);
    }
}
bradlc commented 6 years ago

After chatting to @sambeevors I realised that you can already do this with View::share

bradlc commented 6 years ago

The difference is that stuff shared with View::share is not attached to window/global, it is passed in to your root component as a prop (which is potentially better to be fair)

bradlc commented 6 years ago

Props are now available on window.Laravel