All files / addon/components bootstrap-form.js

0% Statements 0/26
0% Branches 0/8
0% Functions 0/9
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                                                                                                                                           
import { assert } from '@ember/debug';
import Component from '@ember/component';
import Ember from 'ember';
import { task, didCancel } from 'ember-concurrency';
import layout from '../templates/components/bootstrap-form';
import PropTypeMixin, { PropTypes } from 'ember-prop-types';
 
export default Component.extend(PropTypeMixin, {
  layout,
  tagName: 'form',
  propTypes: { // eslint-disable-line ember/avoid-leaking-state-in-ember-objects
    isEditing: PropTypes.bool,
    save: PropTypes.func.isRequired,
    cancel: PropTypes.func,
  },
 
  init() {
    console.warn('DEPRECATION: bootstrap-form component is being replaced by bootstrap/simple-form in an upcoming version of ember-bootstrap-controls.');
    this._super(...arguments);
  },
 
  getDefaultProps() {
    return { isEditing: false };
  },
 
  _isSaving: false,
  _isCanceling: false,
 
  saveTask: task(function * () {
    try {
      this.set('_isSaving', true);
      const saveAction = this.get('save');
 
      if (typeof saveAction === 'function') {
        const rval = yield saveAction(...arguments);
 
        this.set('isEditing', false);
 
        return rval;
      } else {
        assert('Save function required');
      }
    } finally {
      this.set('_isSaving', false);
    }
  }).restartable(),
 
  cancelTask: task(function * () {
    try {
      this.set('_isCanceling', true);
 
      const cancelAction = this.get('cancel');
 
      if (typeof cancelAction === 'function') {
        yield cancelAction(...arguments);
      }
 
      this.set('isEditing', false);
    } finally {
      this.set('_isCanceling', false);
    }
  }),
 
  actions: {
    edit() {
      this.set('isEditing', true);
    },
 
    save() {
      return this.get('saveTask').perform(...arguments).catch(e => {
        if (!didCancel(e)) {
          throw e;
        }
      });
    },
 
    cancel() {
      return this.get('cancelTask').perform(...arguments).catch(e => {
        if (!didCancel(e)) {
          throw e;
        }
      });
    }
  }
});