Skip to content

Instantly share code, notes, and snippets.

@grapho
Forked from poteto/cp-macro.js
Created April 11, 2016 14:47
Show Gist options
  • Save grapho/03776dfc17e0a71814bebb350b98f05a to your computer and use it in GitHub Desktop.
Save grapho/03776dfc17e0a71814bebb350b98f05a to your computer and use it in GitHub Desktop.
When you don't know what keys to observe for a CP, you can use a macro that determines the keys at runtime.
export default function makeComputed(objKey) {
let keys = Object.keys(get(this, objKey))
.filter((key) => {
// choose the keys using some logic
})
.map((key) => `${objKey}.${key}`);
return computed(...keys, {
get() {
// do stuff
}
})
}
export default Component.extend({
myObject: { ... },
stuff: makeComputed('myObject')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment