|
1 | 1 | import { track, defaultTracker, ForEach } from "mutraction-dom";
|
2 | 2 | import { buildData, type RowItem } from "./build-dummy-data.js";
|
3 |
| - |
4 |
| -defaultTracker.setOptions({ trackHistory: false }); |
| 3 | +defaultTracker.setOptions({ trackHistory: false, compactOnCommit: false }); |
5 | 4 |
|
6 | 5 | const model = track({
|
7 |
| - items: [] as RowItem[], |
8 | 6 | selected: undefined as RowItem | undefined,
|
9 | 7 | });
|
| 8 | +const items = track([] as RowItem[]); |
10 | 9 |
|
11 | 10 | function select(item: RowItem) {
|
12 | 11 | model.selected = item;
|
13 | 12 | }
|
14 | 13 |
|
15 | 14 | function create(n: number) {
|
16 |
| - model.items.splice(0, model.items.length, ...buildData(n)); |
| 15 | + items.splice(0, items.length, ...buildData(n)); |
17 | 16 | }
|
18 | 17 |
|
19 | 18 | function append(n: number) {
|
20 |
| - model.items.push(...buildData(n)); |
| 19 | + items.push(...buildData(n)); |
21 | 20 | }
|
22 | 21 |
|
23 | 22 | function update() {
|
24 | 23 | defaultTracker.startTransaction();
|
25 |
| - for (let i = 0; i < model.items.length; i += 10) { |
26 |
| - model.items[i].label += " !!!"; |
| 24 | + for (let i = 0, found = 0; i < items.length; i++) { |
| 25 | + if (!(i in items)) continue; |
| 26 | + if (found++ % 10 === 0) items[i].label += " !!!"; |
27 | 27 | }
|
28 | 28 | defaultTracker.commit();
|
29 | 29 | }
|
30 | 30 |
|
31 | 31 | function clear() {
|
32 |
| - model.items.length = 0; |
| 32 | + items.length = 0; |
33 | 33 | }
|
34 | 34 |
|
35 | 35 | function swapRows() {
|
36 |
| - if (model.items.length > 998) { |
| 36 | + if (items.length > 998) { |
37 | 37 | const i1 = 1, i2 = 998;
|
38 |
| - [model.items[i1], model.items[i2]] = [model.items[i2], model.items[i1]]; |
| 38 | + [items[i1], items[i2]] = [items[i2], items[i1]]; |
39 | 39 | }
|
40 | 40 | }
|
41 | 41 |
|
42 | 42 | function remove(i: number) {
|
43 |
| - model.items.splice(i, 1); |
| 43 | + delete items[i]; |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | const app =
|
@@ -76,7 +76,7 @@ const app =
|
76 | 76 | </div>
|
77 | 77 | <table className="table table-hover table-striped test-data">
|
78 | 78 | <tbody id="tbody">
|
79 |
| - { ForEach(model.items, (item, i) => |
| 79 | + { ForEach(items, (item, i) => |
80 | 80 | <tr classList={{ danger: item === model.selected }}>
|
81 | 81 | <td className="col-md-1">{ item.id }</td>
|
82 | 82 | <td className="col-md-4">
|
|
0 commit comments