qcod / laravel-app-settings

Store settings in database with a manager UI for your Laravel app
MIT License
338 stars 53 forks source link

Best way to access settings from javascript ? #4

Closed naingyy closed 6 years ago

naingyy commented 6 years ago

Hi , Thanks for your great package. I want to ask is which is the best to access setting from javascript. I mean form app.js file or vuejs component.

Thanks!

saqueib commented 6 years ago

Hi, To access setting on the javascript side you should add this in your global scope like laravel does.

// layout.blade.php
<head>
<title>@yield('title', 'Settings')</title>
    <script>
        window.App = {!! json_encode([
               'settings' => \setting()->all(),
               'anyOtherThings' => []
        ]); !!}
    </script>
</head>

Now in your vue component, you can access it App.settings.app_name.

It will be best to define some computed property on the root component and access these global App namespace from there.

const app = new Vue({
    el: '#app',
    computed: {
         myApp() {
            return window.App;
         }
    }
});

// access it 
myApp.settings.app_name
naingyy commented 6 years ago

Thanks @saqueib

Nice solution.I love this solutions.

!Thanks