Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Last active November 1, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonatanKra/6e9e1f714a54e26baab90b85701a3f02 to your computer and use it in GitHub Desktop.
Save YonatanKra/6e9e1f714a54e26baab90b85701a3f02 to your computer and use it in GitHub Desktop.
Final form file and its usage in the main entry
import template from './app.html';
import './app.css';
import {YOPFForm} from "../form/form";
export class App {
constructor(element) {
this._element = element;
element.innerHTML = template;
this.form = this.setupForm(this._element.querySelector('.phrase-form-wrapper'));
}
setupForm(element) {
const form = new YOPFForm(element);
form.listen(this.onPhraseChange);
return form;
}
onPhraseChange(phrase) {
alert(phrase);
}
}
// get the template
import template from './form.html';
// get the styles
import './form.css';
export class YOPFForm{
constructor(element) {
this._element = element;
this.setTemplate();
}
setTemplate() {
this._element.innerHTML = template;
this._form = this._element.getElementsByTagName('form')[0];
this._form.addEventListener("submit", (event)=> {
event.preventDefault();
this.onSubmit(event.target);
}, false);
}
onSubmit(form) {
this._callback(form.phrase.value);
}
listen(callback) {
this._callback = callback;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment