|
| 1 | +import { VDomModel } from '@jupyterlab/apputils'; |
| 2 | +import { |
| 3 | + customElement, |
| 4 | + FASTElement, |
| 5 | + html, |
| 6 | + Observable, |
| 7 | + observable |
| 8 | +} from '@microsoft/fast-element'; |
| 9 | + |
| 10 | +/* |
| 11 | + * Common HTML template |
| 12 | + */ |
| 13 | +const template = html<ObservableView | BasicView>`<div> |
| 14 | + <button |
| 15 | + @click=${x => { |
| 16 | + x.model?.incrementCounter(); |
| 17 | + }} |
| 18 | + > |
| 19 | + Increment |
| 20 | + </button> |
| 21 | + <span>${x => x.model?.counter ?? '0'}</span> |
| 22 | +</div>`; |
| 23 | + |
| 24 | +/* |
| 25 | + * VDOM model with Lumino signal |
| 26 | + */ |
| 27 | + |
| 28 | +export class BasicModel extends VDomModel { |
| 29 | + get counter(): number { |
| 30 | + return this._counter; |
| 31 | + } |
| 32 | + |
| 33 | + incrementCounter(): void { |
| 34 | + this._counter += 1; |
| 35 | + this.stateChanged.emit(); |
| 36 | + } |
| 37 | + private _counter = 0; |
| 38 | +} |
| 39 | + |
| 40 | +@customElement({ |
| 41 | + name: 'jp-test-basic-view', |
| 42 | + template |
| 43 | +}) |
| 44 | +export class BasicView extends FASTElement { |
| 45 | + get model(): BasicModel | null { |
| 46 | + Observable.track(this, 'model'); |
| 47 | + return this._model; |
| 48 | + } |
| 49 | + set model(m: BasicModel | null) { |
| 50 | + this._model?.stateChanged.disconnect(this._onStateChanged, this); |
| 51 | + this._model = m; |
| 52 | + this._model?.stateChanged.connect(this._onStateChanged, this); |
| 53 | + this._onStateChanged(); |
| 54 | + } |
| 55 | + |
| 56 | + private _onStateChanged(): void { |
| 57 | + this.$fastController.notify('model'); |
| 58 | + } |
| 59 | + |
| 60 | + private _model: BasicModel | null = null; |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Model - view using FAST observable |
| 65 | + */ |
| 66 | + |
| 67 | +export class ObservableModel { |
| 68 | + get counter(): number { |
| 69 | + return this._counter; |
| 70 | + } |
| 71 | + |
| 72 | + incrementCounter(): void { |
| 73 | + this._counter += 1; |
| 74 | + } |
| 75 | + |
| 76 | + @observable |
| 77 | + private _counter = 0; |
| 78 | +} |
| 79 | + |
| 80 | +@customElement({ |
| 81 | + name: 'jp-test-observable-view', |
| 82 | + template |
| 83 | +}) |
| 84 | +export class ObservableView extends FASTElement { |
| 85 | + public model: ObservableModel | null = null; |
| 86 | +} |
0 commit comments