Force ID Only In Route/Model/Link-to

Hi there,

For some routes that show a pile of unrelated data, I believe it is standard practice to use Ember.RSVP.hash({...}) in the model hook of the route. This allows calling more than API handler to get data.

However, this only works if you pass an ID (or nothing) in various link-to’s of that route. For example, {{link-to "Dashboard" "dashboard" model.id}} will work, but {{link-to "Dashboard" "dashboard" model}} will not, as it skips the model hook on the router.

Is there any way to enforce that a route is only called with an ID, and not a full model?

You can either ‘reopen’ a link component or extend it and create a new link-to component with params assertion. What does it mean to ‘enforce’? Either log param violation with Ember.Logger or throw new Error.

Hope this answers your question.

Heres the example of reopening component:

import Ember from 'ember';

function assertLinkParams(routeName, params) {
  for (var i = 0; i < params.length; i++) {
  	if (typeof(params[i]) !== 'number' && typeof(params[i]) !== 'string') {
        //throw new Error(`Params passed to link-to on ${routeName} route should be primitive values.`);
    	Ember.Logger.error(`Params passed to link-to on ${routeName} route should be primitive values.`);
        return;
    }
  }
};

export default Ember.LinkComponent.reopen({
  didReceiveAttrs: function() {
    assertLinkParams(this.attrs.params[0], this.attrs.params);
    this._super.apply(this, arguments);  
  }
});

https://ember-twiddle.com/abe8034296c1ea767396?numColumns=1&openFiles=link-component-redifinition.js%2C

Hey - thanks for that! Throwing an error or logging to console are both good options - thanks for including both :slight_smile: