Skip to content

Instantly share code, notes, and snippets.

@mreishus
Last active September 13, 2019 04:52
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 mreishus/3c6d5b8cb7b0ccfbef4a8bdb63e95c50 to your computer and use it in GitHub Desktop.
Save mreishus/3c6d5b8cb7b0ccfbef4a8bdb63e95c50 to your computer and use it in GitHub Desktop.
First try at Phoenix LiveView Hook organization
// 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();
// 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