Skip to content

Commit

Permalink
feat(multilang): add decorator for localized fields
Browse files Browse the repository at this point in the history
  • Loading branch information
velrest committed Jun 30, 2020
1 parent 26b57ce commit ec20d27
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions addon/decorators/localized-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { attr } from "@ember-data/model";

export default function (...args) {
const [target, name] = args;

const attrComputed = attr(...args);
const { get: getter, set: setter } = attrComputed;

target.localizedFields = [...(target.localizedFields || []), name];

attrComputed.get = function () {
const value = getter.call(this);

return value && value[this.getFieldLocale()];
};

attrComputed.set = function (value) {
let attribute = getter.call(this);

if (!attribute) {
attribute = this.intl.locales.reduce(
(accumulator, currentValue) => ({
...accumulator,
[currentValue]: "",
}),
{}
);
}

attribute[this.getFieldLocale()] = value;
setter.call(this, attribute);
};

Object.defineProperty(target, `_${name}`, {
get() {
return getter.call(this);
},
set(value) {
setter.call(this, value);
},
});

return attrComputed;
}
1 change: 1 addition & 0 deletions app/decorators/localized-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-emeis/decorators/localized-attrs";

0 comments on commit ec20d27

Please sign in to comment.