Jump to content

$config->js only working when logged in?


bernhard
 Share

Recommended Posts

i need to share some variables between my backend and frontend. i tried using $config->js for that: https://processwire.com/api/ref/config/js/

the strange thing is that it throws an "Uncaught ReferenceError: ProcessWire is not defined" error - but only when i am not logged in! i tried it on a different install (both pw3) and there is the same problem. is that intended? what can be the problem?

_main.php
[...]
$config->js('test', 'bla');
?>
<script>
  $(document).ready(function() {
    console.log(ProcessWire.config.test);
  });
</script>

ok without the document.ready i get the error both logged in and logged out... even more strange :(

any ideas?

Link to comment
Share on other sites

Js would try to add properties to ProcessWire obj but on front end it's not available. Try var ProcessWire = ProcessWire || {}; but perhaps you should add Js another way instead. At least that's my theory :)

Plus ProcessWire.config will be unavailable too.

  • Like 1
Link to comment
Share on other sites

Why should it not be available on the front-end? It's just you have to output the stuff in your header. Look at the default.php of templates-admin/. I always use it in my sites.

<script type="text/javascript">
	var config = <?php echo json_encode($jsConfig); ?>;
</script>

Then use the config.key in your JS.

  • Like 7
Link to comment
Share on other sites

thank you guys, especially @Soma, helped me a lot!

i totally forgot i had to inject the variable on my own in frontend. when i was logged in it was present because i use frontendediting and there it gets injected automatically. totally makes sense :)

i took a slightly different approach with my own js variable:

// template
    <?php
    echo $modules->get('PwFullCalendar')->render(array(
        'editable' => $page->editable(),
        ));
    ?>

// module
    public function render($options = array()) {

        // get settings
        $settings = array_merge($this->settings, $options);
        extract($settings);

        // push settings to javascript
        $out = '<script>var fullcalsettings = ' . json_encode($settings) . ';</script>';

 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...