Skip to content

Instantly share code, notes, and snippets.

@allthesignals
Created April 30, 2018 19:29
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 allthesignals/059b0fe3da6dd94b322a3a60d33cea50 to your computer and use it in GitHub Desktop.
Save allthesignals/059b0fe3da6dd94b322a3a60d33cea50 to your computer and use it in GitHub Desktop.
Aliased & observed properties from a collection of models
import Service from '@ember/service';
import EmberObject from '@ember/object';
import { alias } from '@ember/object/computed';
export default class DynamicQueryParamsService extends Service {
setup(expected, received) {
// map and alias model prop references to public properties
const aliasedObjectMap = expected
.reduce((acc, layerGroup) => {
const id = layerGroup.get('id');
acc[`_${id}`] = layerGroup;
acc[id] = alias(`_${id}.visible`);
return acc;
}, {});
const keys = expected.mapBy('id');
const _aliasMap = EmberObject.extend(aliasedObjectMap).create();
// coerce types. later: coerce to inferred types
const coercedReceivedParams = {};
Object.keys(received).forEach((key) => {
if (keys.includes(key)) {
coercedReceivedParams[key] = JSON.parse(received[key]);
}
});
// update the new alias map with received types
_aliasMap.setProperties(coercedReceivedParams);
this.setProperties({
keys,
_aliasMap,
});
keys.forEach((key) => {
_aliasMap.addObserver(`${key}`, this, 'pushParamState');
});
window.dynamicQueryParamsService = this;
}
pushParamState(sender, key, value, rev) {
console.log(key, value, rev);
}
// object map for aliases
_aliasMap = null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment