Skip to content

Commit

Permalink
Fix type errors for ElementView
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Aug 7, 2023
1 parent f2c1035 commit c27a610
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/element.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import extend from 'lodash-es/extend.js';
import isFunction from 'lodash-es/isFunction.js';
import pick from 'lodash-es/pick.js';
import uniqueId from 'lodash-es/uniqueId.js';
import { Events } from './events.js';
import { render } from 'lit-html';
import EventEmitter from './eventemitter.js';

// Cached regex to split keys for `delegate`.
const delegateEventSplitter = /^(\S+)\s*(.*)$/;

// List of view options to be set as properties.
const viewOptions = ['model', 'collection', 'events'];

export class ElementView extends HTMLElement {
events = {};
class ElementView extends HTMLElement {
get events() {
return {};
}

/**
* @param {Record<string, any>} options
*/
constructor(options) {
super();

// Will be assigned to from Events
this.stopListening = null;

// Creating a View creates its initial element outside of the DOM,
// if an existing element is not provided...
this.cid = uniqueId('view');
this._domEvents = [];
extend(this, pick(options, viewOptions));
Object.assign(this, pick(options, viewOptions));
}

createRenderRoot() {
Expand All @@ -39,7 +47,7 @@ export class ElementView extends HTMLElement {

disconnectedCallback() {
this.undelegateEvents();
this.stopListening();
this.stopListening?.();
}

// preinitialize is an empty function by default. You can override it with a function
Expand All @@ -53,16 +61,23 @@ export class ElementView extends HTMLElement {
// initialization logic.
initialize() {} // eslint-disable-line class-methods-use-this

beforeRender () {}
afterRender () {}

// **render** is the core function that your view should override, in order
// to populate its element (`this.el`), with the appropriate HTML. The
// convention is for **render** to always return `this`.
render() {
isFunction(this.beforeRender) && this.beforeRender();
isFunction(this.toHTML) && render(this.toHTML(), this);
isFunction(this.afterRender) && this.afterRender();
this.beforeRender();
render(this.toHTML(), this);
this.afterRender();
return this;
}

toHTML () {
return '';
}

// Set callbacks, where `this.events` is a hash of
//
// *{"event selector": "callback"}*
Expand Down Expand Up @@ -179,5 +194,6 @@ export class ElementView extends HTMLElement {
}
}

// Set up all inheritable **View** properties and methods.
Object.assign(ElementView.prototype, Events);
Object.assign(ElementView.prototype, EventEmitter.prototype);

export default ElementView;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { Events } from './events.js';
import History from './history.js';
import { Model } from './model.js';
import { Router } from './router.js';
import ElementView from './element.js';

const skeletor = {
Collection,
ElementView,
Events,
History,
Model,
Expand Down

0 comments on commit c27a610

Please sign in to comment.