@@ -189,11 +189,12 @@ the relationship in [Entity.schema](../api/Entity.md#schema)
189
189
``` ts title="resources/User" collapsed
190
190
export class User extends Entity {
191
191
id = 0 ;
192
+ username = ' ' ;
192
193
name = ' ' ;
193
194
email = ' ' ;
194
195
website = ' ' ;
195
196
pk() {
196
- return ` ${ this .id } ` ;
197
+ return this .id ;
197
198
}
198
199
}
199
200
export const UserResource = resource ({
@@ -208,15 +209,19 @@ import { User } from './User';
208
209
209
210
export class Todo extends Entity {
210
211
id = 0 ;
211
- userId = User .fromJS ({});
212
+ userId = 0 ;
213
+ user? = User .fromJS ({});
212
214
title = ' ' ;
213
215
completed = false ;
214
216
pk() {
215
- return ` ${ this .id } ` ;
217
+ return this .id ;
216
218
}
217
219
static schema = {
218
- userId : User ,
220
+ user : User ,
219
221
};
222
+ static process(value ) {
223
+ return { ... value , user: value .userId };
224
+ }
220
225
}
221
226
export const TodoResource = resource ({
222
227
urlPrefix: ' https://jsonplaceholder.typicode.com' ,
@@ -229,15 +234,14 @@ export const TodoResource = resource({
229
234
import { TodoResource } from ' ./resources/Todo' ;
230
235
import { UserResource } from ' ./resources/User' ;
231
236
232
-
233
237
function TodosPage() {
234
238
useFetch (UserResource .getList );
235
239
const todos = useSuspense (TodoResource .getList );
236
240
return (
237
241
<div >
238
- { todos .slice (17 ,24 ).map (todo => (
242
+ { todos .slice (17 , 24 ).map (todo => (
239
243
<div key = { todo .pk ()} >
240
- { todo .title } by <small >{ todo .userId ?.name } </small >
244
+ { todo .title } by <small >{ todo .user ?.name } </small >
241
245
</div >
242
246
))}
243
247
</div >
@@ -353,7 +357,10 @@ export class User extends Entity {
353
357
... existing ,
354
358
... incoming ,
355
359
posts: [... (existing .posts || []), ... (incoming .posts || [])],
356
- comments: [... (existing .comments || []), ... (incoming .comments || [])],
360
+ comments: [
361
+ ... (existing .comments || []),
362
+ ... (incoming .comments || []),
363
+ ],
357
364
};
358
365
}
359
366
@@ -482,8 +489,13 @@ export default function PostPage({ setRoute }) {
482
489
{ comment .content } { ' ' }
483
490
<small >
484
491
<cite
485
- onClick = { () => setRoute (` user/${comment .commenter .id } ` )}
486
- style = { { cursor: ' pointer' , textDecoration: ' underline' }}
492
+ onClick = { () =>
493
+ setRoute (` user/${comment .commenter .id } ` )
494
+ }
495
+ style = { {
496
+ cursor: ' pointer' ,
497
+ textDecoration: ' underline' ,
498
+ }}
487
499
>
488
500
{ comment .commenter .name }
489
501
{ comment .commenter === post .author ? ' [OP]' : ' ' }
0 commit comments