Update: I made some corrections based on comments.
Let’s say you want to create a computed property to upper case another property, for example:
1 2 3 4 |
first: '', upperFirst: Ember.computed('first', function(){ return this.get('first').toUpperCase(); }) |
Let’s say you want to use this “upper” function in multiple places, you can convert it into a “computed macro”.
Create a macro in a subfolder e.g. utils or macros:
1 |
app/macros/upper.js |
Create the helper:
1 2 3 4 5 6 7 8 9 10 |
import Ember from 'ember'; const { get,computed } = Ember; export default function (key) { return computed(key, function(){ let value = get(this,key); return value.toUpperCase(); }); } |
Now, you can use that computed macro like this:
1 |
upperFirst: upper('first'), |
Don’t forget to import it:
1 |
import upper from '../macros/upper'; |
This tip was inspired by the ember-awesome-macros addon.
Also published on Medium.