You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guide/angular-data/resource/resource.md
+45-1
Original file line number
Diff line number
Diff line change
@@ -301,7 +301,12 @@ DS.defineResource({
301
301
belongsTo: {
302
302
organization: {
303
303
localKey:'organizationId',
304
-
localField:'organization'
304
+
localField:'organization',
305
+
306
+
// if you add this to a belongsTo relation
307
+
// then angular-data will attempt to use
308
+
// a nested url structure, e.g. /organization/15/user/4
309
+
parent:true
305
310
}
306
311
}
307
312
}
@@ -413,6 +418,45 @@ You can configure your server to also return the `comment` and `organization` re
413
418
414
419
If you've told angular-data about the relations, then the comments and organization will be injected into the data store in addition to the user.
415
420
421
+
## Nested Resource Endpoints
422
+
423
+
Add `parent:true` to a belongsTo relationship to activate nested resource endpoints for the resource. Angular-data will attempt to find the appropriate key in order to build the url. If the parent key cannot be found then angular-data will resort to a non-nested url unless you manually provide the id of the parent.
424
+
425
+
Example:
426
+
427
+
```js
428
+
DS.defineResource({
429
+
name:'comment',
430
+
relations: {
431
+
belongsTo: {
432
+
post: {
433
+
parent:true,
434
+
localKey:'postId',
435
+
localField:'post'
436
+
}
437
+
}
438
+
}
439
+
});
440
+
441
+
// The comment isn't in the data store yet, so angular-data wouldn't know
442
+
// what the id of the parent "post" would be, so we pass it in manually
443
+
DS.find('comment', 5, { parentKey:4 }); // GET /post/4/comment/5
444
+
445
+
// vs
446
+
447
+
DS.find('comment', 5); // GET /comment/5
448
+
449
+
DS.inject('comment', { id:1, postId:2 });
450
+
451
+
// We don't have to provide the parentKey here
452
+
// because angular-data found it in the comment
453
+
DS.update('comment', 1, { content:'stuff' }); // PUT /post/2/comment/1
454
+
455
+
// If you don't want the nested for just one of the calls then
0 commit comments