Files
PIbd-21_Permyakov_R.G._IP/components/news/form/controller.js

48 lines
1.1 KiB
JavaScript

import FormModel from "./model";
import FormView from "./view";
class FormElement extends HTMLElement {
constructor() {
super();
this.currentId = null;
const saveCallback = this.save.bind(this);
const backCallback = this.back.bind(this);
this.view = new FormView(this, saveCallback, backCallback);
this.model = new FormModel();
}
async connectedCallback() {
const params = new URLSearchParams(document.location.search);
this.currentId = params.get("id") || null;
await this.model.getTegs();
this.view.render(this.model);
this.get();
}
async get() {
if (this.currentId) {
await this.model.get(this.currentId);
}
this.view.update(this.model);
}
async save() {
if (!this.currentId) {
await this.model.create();
}
else {
await this.model.update();
}
this.view.successToast();
this.view.update(this.model);
}
back() {
window.location.href = "/index";
}
}
customElements.define("news-form", FormElement);