Created
April 30, 2018 19:29
-
-
Save allthesignals/059b0fe3da6dd94b322a3a60d33cea50 to your computer and use it in GitHub Desktop.
Aliased & observed properties from a collection of models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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