github.com/outbrain/consul@v1.4.5/ui-v2/app/mixins/with-blocking-actions.js (about)

     1  import Mixin from '@ember/object/mixin';
     2  import { inject as service } from '@ember/service';
     3  import { get, set } from '@ember/object';
     4  /** With Blocking Actions
     5   * This mixin contains common write actions (Create Update Delete) for routes.
     6   * It could also be an Route to extend but decoration seems to be more sense right now.
     7   *
     8   * Each 'blocking action' (blocking in terms of showing some sort of blocking loader) is
     9   * wrapped in the functionality to signal that the page should be blocked
    10   * (currently via the 'feedback' service) as well as some sane default hooks for where the page
    11   * should go when the action has finished.
    12   *
    13   * Hooks can and are being overwritten for custom redirects/error handling on a route by route basis.
    14   *
    15   * Notifications are part of the injectable 'feedback' service, meaning different ones
    16   * could be easily swapped in an out as need be in the future.
    17   *
    18   */
    19  export default Mixin.create({
    20    _feedback: service('feedback'),
    21    init: function() {
    22      this._super(...arguments);
    23      const feedback = get(this, '_feedback');
    24      const route = this;
    25      set(this, 'feedback', {
    26        execute: function(cb, type, error) {
    27          return feedback.execute(cb, type, error, route.controller);
    28        },
    29      });
    30    },
    31    afterCreate: function(item) {
    32      // do the same as update
    33      return this.afterUpdate(...arguments);
    34    },
    35    afterUpdate: function(item) {
    36      // e.g. dc.intentions.index
    37      const parts = this.routeName.split('.');
    38      // e.g. index or edit
    39      parts.pop();
    40      // e.g. dc.intentions, essentially go to the listings page
    41      return this.transitionTo(parts.join('.'));
    42    },
    43    afterDelete: function(item) {
    44      // e.g. dc.intentions.index
    45      const parts = this.routeName.split('.');
    46      // e.g. index or edit
    47      const page = parts.pop();
    48      switch (page) {
    49        case 'index':
    50          // essentially if I'm on the index page, stay there
    51          return this.refresh();
    52        default:
    53          // e.g. dc.intentions essentially do to the listings page
    54          return this.transitionTo(parts.join('.'));
    55      }
    56    },
    57    errorCreate: function(type, e) {
    58      return type;
    59    },
    60    errorUpdate: function(type, e) {
    61      return type;
    62    },
    63    errorDelete: function(type, e) {
    64      return type;
    65    },
    66    actions: {
    67      cancel: function() {
    68        // do the same as an update, or override
    69        return this.afterUpdate(...arguments);
    70      },
    71      create: function(item) {
    72        return get(this, 'feedback').execute(
    73          () => {
    74            return get(this, 'repo')
    75              .persist(item)
    76              .then(item => {
    77                return this.afterCreate(...arguments);
    78              });
    79          },
    80          'create',
    81          (type, e) => {
    82            return this.errorCreate(type, e);
    83          }
    84        );
    85      },
    86      update: function(item) {
    87        return get(this, 'feedback').execute(
    88          () => {
    89            return get(this, 'repo')
    90              .persist(item)
    91              .then(() => {
    92                return this.afterUpdate(...arguments);
    93              });
    94          },
    95          'update',
    96          (type, e) => {
    97            return this.errorUpdate(type, e);
    98          }
    99        );
   100      },
   101      delete: function(item) {
   102        return get(this, 'feedback').execute(
   103          () => {
   104            return get(this, 'repo')
   105              .remove(item)
   106              .then(() => {
   107                return this.afterDelete(...arguments);
   108              });
   109          },
   110          'delete',
   111          (type, e) => {
   112            return this.errorDelete(type, e);
   113          }
   114        );
   115      },
   116    },
   117  });