Skip to content

Add model definition comparing EmberData models with SchemaRecord #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export default class ApplicationRoute extends Route {
},
],
},
{
id: 'models',
subsections: [
{
id: 'model-definition',
classicFiles: ['old.js'],
octaneFiles: ['new.js'],
},
],
},
];
}
}
70 changes: 70 additions & 0 deletions snippets/models/model-definition/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { withDefaults } from '@warp-drive/schema-record';

// Register schemas for all resources at once
store.schema.registerResources([
// User schema
withDefaults({
type: 'user',
fields: [
{ kind: 'field', name: 'firstName' },
{ kind: 'field', name: 'lastName' },
{
kind: 'derived',
name: 'name',
type: 'concat',
options: { fields: ['firstName', 'lastName'], separator: ' ' }
},
{
kind: 'hasMany',
name: 'pets',
type: 'pet',
options: {
async: false,
inverse: 'owner',
polymorphic: true,
linksMode: true,
resetOnRemoteUpdate: false,
}
}
]
}),

// Pet schema
withDefaults({
type: 'pet',
fields: [
{ kind: 'field', name: 'name' },
{
kind: 'belongsTo',
name: 'owner',
type: 'user',
options: {
async: false,
inverse: 'pets',
linksMode: true,
resetOnRemoteUpdate: false,
}
}
]
}),

// Dog schema (extends pet)
withDefaults({
type: 'dog',
fields: [
{ kind: 'field', name: 'breed' },
{
kind: 'belongsTo',
name: 'owner',
type: 'user',
options: {
async: false,
inverse: 'pets',
as: 'pet',
linksMode: true,
resetOnRemoteUpdate: false,
}
}
]
})
]);
23 changes: 23 additions & 0 deletions snippets/models/model-definition/old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Traditional EmberData Model Definition
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';

export default class UserModel extends Model {
@attr('string') firstName;
@attr('string') lastName;
@hasMany('pet', { polymorphic: true, inverse: 'owner' }) pets;

get name() {
return `${this.firstName} ${this.lastName}`;
}
}

// Related models would be defined in separate files:

// export default class PetModel extends Model {
// @attr('string') name;
// @belongsTo('user', { inverse: 'pets' }) owner;
// }

// export default class DogModel extends PetModel {
// @attr('string') breed;
// }
2 changes: 2 additions & 0 deletions translations/models/en-us.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: Models
description: See how traditional EmberData models compare to the new SchemaRecord approach.
2 changes: 2 additions & 0 deletions translations/models/model-definition/en-us.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: Model Definition
description: See how to define resources using the traditional EmberData models and the new SchemaRecord approach.