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

Any thoughts on ways to make gon play nicely with webpacker? #239

Open
jasonkarns opened this issue Jan 12, 2018 · 4 comments
Open

Any thoughts on ways to make gon play nicely with webpacker? #239

jasonkarns opened this issue Jan 12, 2018 · 4 comments

Comments

@jasonkarns
Copy link

I don't have any suggestions here, but wanted to start a thread about ways to make gon work in a webpacker world... Specifically such that gon could be imported by JS files rather than being a global?

@gamesover
Copy link

gon is a hack. should use ajax/json to fetch the variable

@jasonkarns
Copy link
Author

jasonkarns commented Jul 31, 2018 via email

@kevinluo201
Copy link

Currently, I'm doing this:

  1. in a controller
class MyController < ApplicationController
  def index
    @user = User.first
    gon.user = @user
  end
end
  1. in webpacker's entry file, I pass gon into props
// app/javascript/packs/hello_vue.js
import Vue from 'vue'
import HelloVue from '../views/HelloVue.vue'

window.addEventListener('load', () => {
  const app = new Vue({
    el: document.getElementById('div-you-want-to-mount'),
    components: { HelloVue },
    render: h => h(HelloVue, { props: gon })
  }).$mount()
})
  1. in HelloVue.vue
<template>
  <div id="hello">
    User: {{ user }}
  </div>
</template>

<script>
export default {
  props: ['user']
}
</script>

@mockdeep
Copy link

mockdeep commented Jan 4, 2021

It would be nice to have a way to import gon in our JS. Having it in the global scope creates load order issues. If include_gon is not before your JS include tags, it ends can cause errors on load.

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