A profile form, a header avatar and a settings panel all show the same user. When the name changes in one place, the other two must redraw, and only when something really changed. An observable model keeps the values and tells registered listeners about each change, so views subscribe to the model instead of to each other.
Write the class Model in model.js. It stores named values, and every set call that changes something fires "change:<key>" events for each changed key and one "change" event for the whole call.
classModel {constructor(initial) // initial: an object, optionalget(key) // the value, or undefinedset(key, value) // returns the modelset(changes) // changes: an object of keys and values; returns the modelon(event, listener) // returns a function that unregisters the listenertoJSON() // a new plain object of every key and value}
Listeners are called as:
"change:<key>": listener(value, previous, model)
"change": listener(changes, model), where changes holds only the keys that changed, with their new values.
The model starts with a copy of initial's keys and values (empty without it); later changes to initial do not affect it. get returns undefined for a missing key.
set(key, value) and set(changes) store the values and return the model.
A key the model does not have yet has changed when it is set, whatever the value, undefined included: the key now exists, and toJSON lists it. A key the model already has has changed when Object.is(old value, new value) is false: setting the same value (or the same object) fires nothing, NaN to NaN is no change, 0 to -0 is a change.
For each changed key, "change:<key>" listeners run with the new value, the old value and the model. A listener for one key never runs for another.
After those, "change" listeners run once per set call, with an object of only the changed keys. A call that changes nothing fires no "change" at all.
Events fire only after every value of the call is stored, in the order of the keys, with "change" last.
on returns a function that unregisters that one registration; calling it again does nothing. The same function registered twice runs twice, and one unregister leaves the other.
The listeners of an event are the ones registered when it started: a listener that unregisters itself does not make the next one get skipped, and a listener added during an event first runs on the next one.
Each model has its own values and listeners.
toJSON() returns a new plain object each time, so JSON.stringify(model) works and changing the copy does not change the model.
Every string is an ordinary key, including "__proto__" and "toString".
A set call made inside a listener stores its values and fires its own events.
on throws a TypeError when the listener is not a function.
constuser=newModel({ name: "Ana", age: 30 });user.on("change:name", (value, previous) => console.log(previous, "->", value));user.on("change", (changes) => console.log("changed", changes));user.set("name", "Ben");// "Ana -> Ben"// "changed { name: 'Ben' }"user.set({ name: "Ben", age: 31 });// "changed { age: 31 }" (name did not change, so no change:name)user.set("age", 31);// nothing: no changeuser.set("nickname", undefined);// "changed { nickname: undefined }" (a new key is a change, even with the value undefined)