ember-learn / ember-learn/guides-source

Octane @tracked properties in native classes models do not JSON.stringify

Open
#1,138 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
161
Forks
512
Avg merge
4d 9h
Merged PRs (30d)
4

Description

declaring models as util classes and using `@tracked` property (as described here https://octane-guides-preview.emberjs.com/release/state-management/tracked-properties/#toc_tracked-properties-in-custom-classes) causes any such model passed to a `JSON.stringify` call to omit any tracked properties from the json output.
in short doing this
```javascript
class Person {
@tracked firstName;
@tracked lastName;

constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
JSON.stringify(new Person('Tobias', 'Bieniek')); // --> "{}"
```

solution would be implementing `toJSON` method in the class

the guides should be updated to reflect that as it's very likely someone would want to pass those models to a persistence layer of any sort (this would be a problem even with localStorage)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.