Published
Edited
Oct 21, 2019
Insert cell
Insert cell
viewof x = html`<input type=range>`
Insert cell
x
Insert cell
Insert cell
class View {
constructor(value) {
Object.defineProperties(this, {
_list: {value: [], writable: true},
_value: {value, writable: true}
});
}
get value() {
return this._value;
}
set value(value) {
this._value = value;
this.dispatchEvent({type: "input", value});
}
addEventListener(type, listener) {
if (type != "input" || this._list.includes(listener)) return;
this._list = [listener].concat(this._list);
}
removeEventListener(type, listener) {
if (type != "input") return;
this._list = this._list.filter(l => l !== listener);
}
dispatchEvent(event) {
const p = Promise.resolve(event);
this._list.forEach(l => p.then(l));
}
}
Insert cell
Insert cell
viewof y = new View(42)
Insert cell
Insert cell
y
Insert cell
Insert cell
{
const input = html`<input type=range min=0 max=100 step=1>`;
input.oninput = () => viewof y.value = input.valueAsNumber;
const update = () => input.value = viewof y.value;
viewof y.addEventListener("input", update);
invalidation.then(() => viewof y.removeEventListener("input", update));
return update(), input;
}
Insert cell
Insert cell
function bind(input, view) {
input.value = view.value;
input.oninput = () => view.value = input.valueAsNumber;

function update({value}) {
if (!isNaN(value) && input.valueAsNumber !== value) {
input.value = value;
}
}

view.addEventListener("input", update);
return Generators.disposable(input, () => {
view.removeEventListener("input", update);
});
}
Insert cell
Insert cell
bind(html`<input type=range min=0 max=100 step=1>`, viewof y)
Insert cell
bind(html`<input type=number min=0 max=100 step=1>`, viewof y)
Insert cell
y
Insert cell
bind(html`<input type=range min=0 max=100 step=1>`, viewof y)
Insert cell
import {slider} from "@embracelife/inputs"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more