Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active February 14, 2019 12:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aschen/f0a53b0d522688cc46ef77705074c1d2 to your computer and use it in GitHub Desktop.
Save Aschen/f0a53b0d522688cc46ef77705074c1d2 to your computer and use it in GitHub Desktop.
Import javascript class in the standard asset pipeline
// app/javascript/packs/application.js
import Foobar from '../foobar/foobar'
window.Foobar = Foobar
# app/assets/javascripts/application.js.coffee
#= require jquery
#= require jquery_ujs
#= require_tree .
// Test this in your browser
var test = new Test()
test.hello()
var foobar = new Foobar()
foobar.hello()
// app/javascript/foobar/foobar.js
export default class Foobar {
hello() {
console.log("Hello from Foobar")
}
}
// app/views/home/index.html.slim
= javascript_pack_tag "application"
javascript:
var foobar = new Foobar();
foobar.hello();
# app/assets/javascripts/test.js.coffee
class @Test # For coffeescript @Test is the same as window.Test
constructor: () ->
console.log("I'm Test")
@foobar = new window.Foobar()
hello: ->
@foobar.hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment