Skip to content

Instantly share code, notes, and snippets.

@chrismllr
Created September 18, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismllr/08a0142337f868f231ee75aa1c70d2e6 to your computer and use it in GitHub Desktop.
Save chrismllr/08a0142337f868f231ee75aa1c70d2e6 to your computer and use it in GitHub Desktop.
ember @arg decorator
/**
* @description Declares a one-way read-only component property, which reads from `this.args`
* and falls back to default declared on the class property.
* @export
* @returns {ClassProperty}
*/
export default function arg(target, property, descriptor) {
let _value;
if (descriptor.initializer !== null) {
_value = descriptor.initializer();
}
const newDescriptor = {
get() {
if (property in this.args) {
return this.args[property];
}
if (!('_value' in newDescriptor)) {
newDescriptor._value = _value;
}
return newDescriptor._value;
},
enumerable: true,
configurable: true
};
return newDescriptor;
}
@chrismllr
Copy link
Author

chrismllr commented Sep 18, 2019

Helps with the ambiguity of whether to reference @aProperty or this.aProperty in templates. By this standard, if your component has a javascript file, you'll use this., and if it is template-only, you'll use @

Usage

export default SomeComponent extends Component {
  @arg aProperty = 'defaultValue';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment