Skip to content

Commit 2593689

Browse files
committed
Modify array using .splice() to allow FAST to track its changes, and avoid manual re-rendering with .slice()
1 parent 07d63f6 commit 2593689

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

frameworks/keyed/fast/src/App.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const template = html<BenchmarkApp>`
2424
html`
2525
<data-table
2626
:rows=${x => x.rows}
27-
:selectedRowId=${x => x.selectedRowId}
2827
@action=${(x, c) => {
2928
x.onAction(c.event);
3029
}}
@@ -66,27 +65,37 @@ export class BenchmarkApp extends FASTElement {
6665
if (!this.rows) return;
6766

6867
for (let i = 0; i < this.rows.length; i += 10) {
69-
this.rows[i].label += ' !!!';
68+
// make a copy, then update the array using slice. See below for details
69+
// https://www.fast.design/docs/fast-element/observables-and-state/#observing-arrays
70+
let rowItem = Object.create(this.rows[i]);
71+
rowItem.label += ' !!!';
72+
this.rows.splice(i, 1, rowItem);
7073
}
71-
72-
this.triggerRerender();
7374
}
7475

7576
deleteAllRows() {
7677
this.rows = [];
7778
}
7879

80+
/**
81+
* The observation system cannot track changes made directly
82+
* through an index update. e.g. arr[3] = 'new value';.
83+
* This is due to a limitation in JavaScript.
84+
*
85+
* To work around this, update arrays with the
86+
* equivalent splice code e.g. arr.splice(3, 1, 'new value');
87+
*
88+
* https://www.fast.design/docs/fast-element/observables-and-state/#observing-arrays
89+
*/
7990
swapTwoRows() {
8091
if (!this.rows) return;
8192

8293
if (this.rows.length > 998) {
8394
const secondRow = this.rows[1];
8495
const secondToLastRow = this.rows[998];
85-
this.rows[1] = secondToLastRow;
86-
this.rows[998] = secondRow;
96+
this.rows.splice(1, 1, secondToLastRow);
97+
this.rows.splice(998, 1, secondRow);
8798
}
88-
89-
this.triggerRerender();
9099
}
91100

92101
deleteSingleRow(rowId: number) {
@@ -112,10 +121,4 @@ export class BenchmarkApp extends FASTElement {
112121

113122
throw new Error('unknown event name!');
114123
}
115-
116-
private triggerRerender() {
117-
if (!this.rows) return;
118-
// trigger an update
119-
this.rows = this.rows.slice();
120-
}
121124
}

frameworks/keyed/fast/src/components/Table.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,7 @@ const template = html<Table>`
1919
</td>
2020
<td class="col-md-6"></td>
2121
</tr>
22-
`,
23-
/**
24-
* List Rendering without view recycling
25-
*
26-
* With recycle set to false,
27-
* Fast will re-render when internal properties of
28-
* RowItem[] change (e.g. id or label)
29-
*
30-
* https://www.fast.design/docs/fast-element/using-directives
31-
*/
32-
{ recycle: false }
22+
`
3323
)}
3424
</tbody>
3525
</table>

0 commit comments

Comments
 (0)