Last active
September 13, 2019 04:52
-
-
Save mreishus/3c6d5b8cb7b0ccfbef4a8bdb63e95c50 to your computer and use it in GitHub Desktop.
First try at Phoenix LiveView Hook organization
This file contains hidden or 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
// assets/js/app.js | |
// We need to import the CSS so that webpack will load it. | |
// The MiniCssExtractPlugin is used to separate it out into | |
// its own CSS file. | |
import css from "../css/app.css" | |
// webpack automatically bundles all modules in your | |
// entry points. Those entry points can be configured | |
// in "webpack.config.js". | |
// | |
// Import dependencies | |
// | |
import "phoenix_html" | |
// Import local files | |
// | |
// Local files can be imported directly using relative paths, for example: | |
// import socket from "./socket" | |
import {Socket} from "phoenix" | |
import LiveSocket from "phoenix_live_view" | |
let Hooks = {}; | |
import PhoneNumber from "./hooks/phone_number"; | |
Hooks.PhoneNumber = PhoneNumber; | |
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks}); // Updated for liveview 0.2 | |
liveSocket.connect(); |
This file contains hidden or 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
// assets/js/hooks/phone_number.js | |
let PhoneNumber = { | |
mounted() { | |
console.log("mounted running"); | |
this.el.addEventListener("input", e => { | |
console.log("match running"); | |
let match = this.el.value | |
.replace(/\D/g, "") | |
.match(/^(\d{3})(\d{3})(\d{4})$/); | |
if (match) { | |
console.log("trying to set"); | |
this.el.value = `${match[1]}-${match[2]}-${match[3]}`; | |
} | |
}); | |
} | |
}; | |
export default PhoneNumber; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment