github.com/outbrain/consul@v1.4.5/ui-v2/app/controllers/dc/intentions/edit.js (about)

     1  import Controller from '@ember/controller';
     2  import { get, set } from '@ember/object';
     3  import Changeset from 'ember-changeset';
     4  import lookupValidator from 'ember-changeset-validations';
     5  
     6  import validations from 'consul-ui/validations/intention';
     7  
     8  export default Controller.extend({
     9    setProperties: function(model) {
    10      this.changeset = new Changeset(model.item, lookupValidator(validations), validations);
    11      const sourceName = get(model.item, 'SourceName');
    12      const destinationName = get(model.item, 'DestinationName');
    13      let source = model.items.findBy('Name', sourceName);
    14      let destination = model.items.findBy('Name', destinationName);
    15      if (!source) {
    16        source = { Name: sourceName };
    17        model.items = [source].concat(model.items);
    18      }
    19      if (!destination) {
    20        destination = { Name: destinationName };
    21        model.items = [destination].concat(model.items);
    22      }
    23      this._super({
    24        ...model,
    25        ...{
    26          item: this.changeset,
    27          SourceName: source,
    28          DestinationName: destination,
    29        },
    30      });
    31    },
    32    actions: {
    33      createNewLabel: function(term) {
    34        return `Use a future Consul Service called '${term}'`;
    35      },
    36      isUnique: function(term) {
    37        return !get(this, 'items').findBy('Name', term);
    38      },
    39      change: function(e, value, _target) {
    40        // normalize back to standard event
    41        const target = e.target || { ..._target, ...{ name: e, value: value } };
    42        let name, selected;
    43        name = selected = target.value;
    44        // TODO:
    45        // linter needs this here?
    46        let match;
    47        switch (target.name) {
    48          case 'Description':
    49          case 'Action':
    50            set(this.changeset, target.name, target.value);
    51            break;
    52          case 'SourceName':
    53          case 'DestinationName':
    54            if (typeof name !== 'string') {
    55              name = get(target.value, 'Name');
    56            }
    57            // linter doesn't like const here
    58            match = get(this, 'items').filterBy('Name', name);
    59            if (match.length === 0) {
    60              selected = { Name: name };
    61              // linter doesn't mind const here?
    62              const items = [selected].concat(this.items.toArray());
    63              set(this, 'items', items);
    64            }
    65            set(this.changeset, target.name, name);
    66            set(this, target.name, selected);
    67            break;
    68        }
    69        this.changeset.validate();
    70      },
    71    },
    72  });