Last active
February 14, 2019 12:41
-
-
Save Aschen/f0a53b0d522688cc46ef77705074c1d2 to your computer and use it in GitHub Desktop.
Import javascript class in the standard asset pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/javascript/packs/application.js | |
import Foobar from '../foobar/foobar' | |
window.Foobar = Foobar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/assets/javascripts/application.js.coffee | |
#= require jquery | |
#= require jquery_ujs | |
#= require_tree . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test this in your browser | |
var test = new Test() | |
test.hello() | |
var foobar = new Foobar() | |
foobar.hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/javascript/foobar/foobar.js | |
export default class Foobar { | |
hello() { | |
console.log("Hello from Foobar") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/home/index.html.slim | |
= javascript_pack_tag "application" | |
javascript: | |
var foobar = new Foobar(); | |
foobar.hello(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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