Hijacking action bubbling in routes


#1

I have a scenario where I want to handle an action in a subroute, augment the data and arguments, and then let the parent route handle the action. The case is cloning a record. The template/components send the data and the parent record on save (parent will be null when creating a new record from scratch). The clone works by setting a parent record for the templates/components to base the data off of. On save, the clone route’s save handler does a this.send('save', data) (effectively calling itself). The second time the action handler is called, the parent will be undefined, and the handler returns true so that the action will bubble to the parent. Here is the code:

// app/router.js
export default Ember.Router.map(function() {
  this.route('reports', function() {
    this.route('clone', { path: ':report_id/clone' });
  });
});

// app/routes/reports.js
export default Ember.Route.extend({
  actions: {
    save: function(data, parent) {
      // logic involved in creating a record and saving it.
      if (parent) {
        // attach parent record to newly created record before finally saving
      }
    }
  }
});

// app/routes/reports/clone.js
export default Ember.Route.extend({
  actions: {
    save: function(data, parent) {
      if (parent) {
        this.send('save', data);
      } else {
        return true;
      }
    }
  }
});

This currently works as I need it to, but I wonder if: 1) should this work, 2) is there a better way to do this, 3) will this fail in the future.