Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 609bc83

Browse files
committed
Webpacker image in JS example
1 parent 9aa8045 commit 609bc83

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

app/javascript/packs/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Rails from "@rails/ujs"
77
import Turbolinks from "turbolinks"
88
import * as ActiveStorage from "@rails/activestorage"
99
import "channels"
10+
import "../src/change-background"
1011

1112
const images = require.context("../images", true)
1213
const imagePath = name => images(name, true)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import avatarUrl from '../images/avatar.png'
2+
3+
document.addEventListener(
4+
"turbolinks:load",
5+
() => {
6+
document.getElementById('add-avatar-button').onclick = () => {
7+
const image = new Image();
8+
image.src = avatarUrl;
9+
document.getElementById('so-many-avatars').appendChild(image);
10+
}
11+
}
12+
)

app/javascript/stylesheets/global.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ body {
2323
// TRICKY: ensure all child nodes' margins stay inside container
2424
padding: 1px;
2525
}
26+
27+
section {
28+
margin: 2em 1em 0;
29+
}
30+
31+
button {
32+
background-color: #1663b2;
33+
border: 0;
34+
border-radius: 0.25em;
35+
color: #fff;
36+
padding: 1em;
37+
}

app/views/application/show.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,21 @@
5858
vie.
5959
</p>
6060
</section>
61+
62+
<section>
63+
<h2>On image loading within JS:</h2>
64+
<p>
65+
Bjork is one of those artists that I always forget about, but am always
66+
stoked to return to after a hiatus. 'Post' truly is an incredible album,
67+
and features 'Army of Me', which has a music video containing what I can
68+
only assume was the inspiration for that Tesla truck everyone mocked to
69+
death. Very PS1.
70+
</p>
71+
<p>
72+
Speaking of 'Army of Me', the button below will use Webpacker-loaded
73+
images within Webpacker-loaded JS to do that thing.
74+
</p>
75+
<button id="add-avatar-button" type="button">I MUST HAVE MORE</button>
76+
<div id="so-many-avatars"></div>
77+
</section>
6178
</article>

0 commit comments

Comments
 (0)