This page is part of a static HTML representation of TriTarget.org at https://tritarget.org

Ember queryParams with CSV and object flags

Sukima22nd May 2018 at 9:40pm

This is clever and neat way to convert between an Ember queryParams as a comma separated list and an object that has all the list values as boolean flags. Demo

const KNOWN_PARAMS = ['foo', 'bar', 'baz'];

const FieldParams = EmberObject.extend({
  params: computed(...KNOWN_PARAMS, {
    get() {
      return KNOWN_PARAMS
        .filter(field => this.get(field))
        .join(',');
    },
    set(key value) {
      for (let field of KNOWN_FIELDS) {
        this.set(field, value.includes(field));
      }
      return value;
    }
  })
});

export default Controller.extend({
  queryParams: ['myParams'],
  myParams: alias('fieldParams.params'),
  fieldParams: computed(function() {
    return FieldParams.create();
  })
});

See it in action