Skip to content

Commit

Permalink
fix(model): use objectDefineProperty for localized attributes
Browse files Browse the repository at this point in the history
fixes the bug when using multiple localizedAttr in one model only the
last one would correctly work.
Reason being that spreading an object with getters/setters does not
work.
  • Loading branch information
Yelinz authored and derrabauke committed May 22, 2023
1 parent 521ae98 commit 1b1b05b
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions addon/-private/models/localized.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ export default class LocalizedModel extends Model {

get localizedObjects() {
if (!this._localizedObjects) {
// Save this since the getter will have the
// context from the `_localizedObjects` object.
const context = this;
this._localizedObjects = this.localizedFields.reduce(
(localizedObjects, field) => ({
...localizedObjects,
get [field]() {
return context[`_${field}`];
// this is not in the constructor because
// otherwise the environment cannot be resolved in the decorator
this._localizedObjects = {};
this.localizedFields.forEach((field) =>
Object.defineProperty(this._localizedObjects, field, {
get: () => {
return this[`_${field}`];
},
set [field](value) {
context[`_${field}`] = value;
set: (value) => {
this[`_${field}`] = value;
},
}),
{}
})
);
}

return this._localizedObjects;
}

Expand Down

0 comments on commit 1b1b05b

Please sign in to comment.