Last active
January 20, 2020 04:05
-
-
Save napcs/c86ae7960d69ef59d259 to your computer and use it in GitHub Desktop.
Per-view JavaScript functions for Phoenix. Keeps things out of global space, allows a page to specifically invoke the JS it needs.
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
import {Socket} from "phoenix"; | |
let socket = new Socket("/ws"); | |
let App = { | |
mainPage: function(data){ | |
console.log(data); | |
}, | |
anotherPage: function(data){ | |
console.log(data); | |
} | |
}; | |
// Look for any onload messages, give it the App | |
// context | |
if(window.appConfig !== undefined) { | |
window.appConfig.onLoad.call(this, App); | |
} | |
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
<body> | |
<div class="container"> | |
<div class="header"> | |
<h1>YourApp</h1> | |
</div> | |
<main> | |
<%= @inner %> | |
</main> | |
<div class="footer"> | |
</div> | |
</div> <!-- /container --> | |
<script src="<%= static_path(@conn, "/js/app.js") %>"></script> | |
<script>require("web/static/js/app")</script> | |
</body> |
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
<h1>Hello</h1> | |
<script> | |
window.appConfig = { | |
onLoad: function(App) { | |
App.mainPage({ | |
id: "<%= @id %>", | |
thing: "wat", | |
anotherThing: "AlsoWat" | |
}); | |
} | |
}; | |
</script> |
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
How to load page-specific JS functions and pass values to those functions when JS is loaded below your views. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example is exactly what I need! Thank you so much for posting this. Great job!