This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/ui/web/general/TemplateView.js
2019-10-12 21:16:48 +02:00

40 lines
996 B
JavaScript

import Template from "./Template.js";
export default class TemplateView {
constructor(vm, bindToChangeEvent) {
this.viewModel = vm;
this._changeEventHandler = bindToChangeEvent ? this.update.bind(this, this.viewModel) : null;
this._template = null;
}
render() {
throw new Error("render not implemented");
}
mount() {
if (this._changeEventHandler) {
this.viewModel.on("change", this._changeEventHandler);
}
this._template = new Template(this.viewModel, (t, value) => this.render(t, value));
return this.root();
}
root() {
return this._template.root();
}
unmount() {
if (this._changeEventHandler) {
this.viewModel.off("change", this._changeEventHandler);
}
this._template.dispose();
this._template = null;
}
update(value, prop) {
if (this._template) {
this._template.update(value);
}
}
}