0

I am using Ember 3.18, I am facing the below issue. Consider the following routes:

Router.map(function() {
  this.route('author');
  this.route('author' , {path:"/author/:author_id"});
});

Now, in my hbs file, I am trying to transition to the above routes using a single LinkTo. As you can see, only the second route requires model attribute. In simple terms, I want to combine the below 2 into a single line.

<LinkTo @route="author" />
<LinkTo @route="author" @model="2" />

As you can see, I require the model attribute to be gone in certain cases and availble in certain cases.

Please help.

Panther Coder
  • 1,058
  • 1
  • 16
  • 43

1 Answers1

3

I think the easiest way forward is to tweak your routing setup a bit. I know you want to combine the routes, but it's hard/confusing, imo, and would be "more standard" to do something more traditional like:

Router.map(function() {
  this.route('author', function() {
    this.route('view', {path:":author_id"});
  });
});

and

<LinkTo @route="author.index" />
<LinkTo @route="author.view" @model="2" />

author.index would match /author and author.view (with a @model) would match /author/2.

Note that index is an implicit convention of the web, and not needed in the router.js file

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • I understand that tweaking the routing setup helps here. But, I am still curious about combining the two into a single line. Like by using the `let` helper and based on some condition, include the model otherwise remove it. Is it possible? – Panther Coder Feb 24 '22 at 20:23
  • maybe if your plain route was `author*`, but it _feels like_, an attempt to trick the route pattern matcher – NullVoxPopuli Feb 24 '22 at 20:48