|
| 1 | +# LinksMode |
| 2 | + |
| 3 | +- ⮐ [Relationships Guide](../index.md) |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +**LinksMode** is a special feature that can be activated for any `belongsTo` or `hasMany` relationship. |
| 8 | + |
| 9 | +It allows that relationship to be fetched using the standard `request` experience instead of via the legacy `adapter` interface. |
| 10 | + |
| 11 | +LinksMode behaves *slightly* differently depending on whether |
| 12 | +you are using Model (including via [Legacy Mode](../../reactive-data/legacy/overview.md)) or [Polaris Mode](../../reactive-data/polaris/overview.md). We'll explain this nuance below. |
| 13 | + |
| 14 | +> [!TIP] |
| 15 | +> The next-generation of reactive data which replaces Model is SchemaRecord. |
| 16 | +> SchemaRecord has two modes, Legacy - which emulates all of Model's |
| 17 | +> behaviors and APIs, and Polaris - a new experience which we intend |
| 18 | +> to make default in Version 6. |
| 19 | +
|
| 20 | +## Activating LinksMode |
| 21 | + |
| 22 | +#### For A Relationship on a Model |
| 23 | + |
| 24 | +Add `linksMode: true` to the relationship's options. |
| 25 | + |
| 26 | +```ts |
| 27 | +import Model, { belongsTo, hasMany } from '@ember-data/model'; |
| 28 | + |
| 29 | +export default class User extends Model { |
| 30 | + @belongsTo('address', { |
| 31 | + async: false, |
| 32 | + inverse: 'residents', |
| 33 | + linksMode: true |
| 34 | + }) |
| 35 | + homeAddress; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +This works for both `async` and `non-async` relationships and only changes the fetching behavior the field it is defined on. So for instance, in the example above, `homeAddress` is fetched in links mode while `<Address>.residents` might still be using the legacy adapter experience. |
| 40 | + |
| 41 | +#### For A SchemaRecord in LegacyMode |
| 42 | + |
| 43 | +```ts |
| 44 | +import type { ResourceSchema } from '@warp-drive/core-types/schema/fields'; |
| 45 | + |
| 46 | +const UserSchema = { |
| 47 | + type: 'user', |
| 48 | + // this is what puts the record instance into legacy mode |
| 49 | + legacy: true, |
| 50 | + fields: [ |
| 51 | + { |
| 52 | + kind: 'belongsTo', |
| 53 | + name: 'homeAddress', |
| 54 | + options: { |
| 55 | + async: false, |
| 56 | + inverse: 'residents', |
| 57 | + linksMode: true |
| 58 | + } |
| 59 | + } |
| 60 | + ] |
| 61 | +} satisfies ResourceSchema; |
| 62 | +``` |
| 63 | + |
| 64 | +The behavior of relationships for a SchemaRecord in LegacyMode is always identical to that of Model's. |
| 65 | + |
| 66 | +#### For A SchemaRecord in PolarisMode |
| 67 | + |
| 68 | +```ts |
| 69 | +import type { ResourceSchema } from '@warp-drive/core-types/schema/fields'; |
| 70 | + |
| 71 | +const UserSchema = { |
| 72 | + type: 'user', |
| 73 | + fields: [ |
| 74 | + { |
| 75 | + kind: 'belongsTo', |
| 76 | + name: 'homeAddress', |
| 77 | + options: { |
| 78 | + async: false, |
| 79 | + inverse: 'residents', |
| 80 | + linksMode: true |
| 81 | + } |
| 82 | + } |
| 83 | + ] |
| 84 | +} satisfies ResourceSchema; |
| 85 | +``` |
| 86 | + |
| 87 | +The only difference here is that we don't mark the resource schemas as `legacy`. This puts us in the standard/default mode (`polaris`); |
| 88 | + |
| 89 | +Async relationships are not supported in Polaris mode! |
0 commit comments