Encountering the error
cannot read property 'setDirtyAttribute' of null
even when using YourModel.create({...})
in ember-typescript-cli
to instantiate an 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}`;
}
}
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));
}
}
Error message upon entering 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)
As a solution, using
this.store.createRecord('person', {firstName: "first1", lastName: "last1", age: 10})
instead of this.store.createRecord('person', <a Person class instance>)
is required, deviating from TypeScript standards. Seeking advice on a more elegant way to merge ember features with TypeScript without resorting to any
or plain object
.
Any suggestions?