Facing cannot read property 'setDirtyAttribute' of null
even if you use YourModel.create({...})
in ember-typescript-cli
to create a EmberObject.
Model:
import DS from 'ember-data';
import {computed} from "@ember/object";
export default class Person extends DS.Model {
@DS.attr() firstName!: string;
@DS.attr() lastName!: string;
@DS.attr() age!: number;
@DS.attr() desc?: string;
@computed("firstName", "lastName")
public get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
// DO NOT DELETE: this is how TypeScript knows how to look up your models.
declare module 'ember-data/types/registries/model' {
export default interface ModelRegistry {
'person': Person;
}
}
Route:
export default class Persons extends Route {
@service() public store!: DS.Store;
constructor() {
super(...arguments);
const persons: Person[] = [
Person.create({firstName: "first1", lastName: "last1", age: 10}),
Person.create({firstName: "first2", lastName: "last2", age: 320}),
Person.create({firstName: "first3", lastName: "last3", age: 30}),
];
persons.forEach(p => this.store.createRecord('person', p));
}
model() {
return this.store.peekAll('person');
}
}
Get error when enter the page:
Uncaught TypeError: Cannot read property 'setDirtyAttribute' of null
at Person.set (-private.js:144)
at ComputedProperty._set (metal.js:3543)
at ComputedProperty.setWithSuspend (metal.js:3532)
at ComputedProperty.set (metal.js:3503)
at initialize (core_object.js:67)
at Function.create (core_object.js:692)
at new Persons (persons.js:23)
at Function.create (core_object.js:684)
at FactoryManager.create (container.js:549)
at instantiateFactory (container.js:359)
So I have to use things like this.store.createRecord('person', {firstName: "first1", lastName: "last1", age: 10})
rather than this.store.createRecord('person', <a Person class instance>)
, which is not typescript-ish at all.
Any advice?