I am migrating my codebase to svelte. My business logic contains a lot of classes, which depend on each other. I created a REPL to visualize my problem: Link.
In this example, the money attribute of a person is only updated correctly in the UI if it is directly changed as a html-inline function (left button). When calling a class method (right button), the state of the class is updated, the UI is not.
I am aware this behaviour is intended, and writables should be used to achieve reactivity outside svelte components. Is it somehow possible to achieve reactivity by using classes like in the REPL? Do I need to replace their attributes with writables in order to fix my REPL? If so, how?
<div id="app">
{#each teams as team}
<div>
{#each team.people as person}
<li class="person-row">
<div>{person.name}</div>
<div>{person.money}€</div>
<button on:click={() => (person.money += 1)}>Add 1€ </button>
<button on:click={() => person.addMoney(1)}>Add 1€ </button>
</li>
{/each}
</div>
{/each}
</div>
<script>
class Person {
constructor(name, money) {
this.name = name;
this.money = money;
}
addMoney(x) {
this.money += x;
}
}
class Team {
constructor(name, people) {
this.name = name;
this.people = people;
}
}
const p1 = new Person("Person 1", 1000);
const p2 = new Person("Person 2", 2);
const p3 = new Person("Person 3", 60);
const p4 = new Person("Person 4", 15);
const t1 = new Team("Team 1", [p1, p2]);
const t2 = new Team("Team 2", [p3, p4]);
const teams = [t1, t2];
</script>
<style>
.person-row {
display: flex;
flex-direction: row;
}
.person-row * {
width: 100px;
}
</style>