在 ember-cli-mirage 中生成 StaffMember 的实例时,我试图创建一个“任务”并将其分配给 StaffMember 模型中的键“tasksCreated”。我当前的代码如下;
它正在创建一个任务,并创建一个 StaffMember,但两者之间没有建立任何关系。
应用程序/模型/staff-member.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
atWork: DS.attr('boolean'),
avatar: DS.attr(),
tasksCreated: DS.hasMany('task', {
inverse: 'creator'
}),
tasksAssigned: DS.hasMany('task', {
inverse: 'assignee'
})
});
应用程序/模型/tasks.js
import DS from 'ember-data';
export default DS.Model.extend({
creator: DS.belongsTo('staff-member', { inverse: null }),
assignee: DS.belongsTo('staff-member', { inverse: null }),
creationDate: DS.attr(),
description: DS.attr('string'),
urgency: DS.attr()
});
海市蜃楼/模型/staff-member.js
import {Model, hasMany} from 'ember-cli-mirage';
export default Model.extend({
tasksCreated: hasMany('task'),
tasksAssigned: hasMany('task', { inverse: 'assignee'})
});
海市蜃楼/模型/task.js
import {Model, belongsTo} from 'ember-cli-mirage';
export default Model.extend({
creator: belongsTo('staff-member'),
assignee: belongsTo('staff-member')
});
海市蜃楼/工厂/staff-member.js
import { Factory, faker, trait } from 'ember-cli-mirage';
export default Factory.extend({
name: faker.name.firstName,
atWork: faker.random.boolean,
avatar: faker.image.avatar,
withTasks: trait({
afterCreate(staffMember, server){
server.createList('task',2,{tasksCreated: [staffMember]});
}
})
});
海市蜃楼/工厂/task.js
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
creationDate: faker.date.recent,
description: faker.lorem.sentence,
urgency: faker.random.number({
'min': 0,
'max': 4
})
});
海市蜃楼/场景/default.js
export default function(server ) {
server.createList('staff-member', 4, 'withTasks');
}