Panther Coder
Panther Coder

Reputation: 1078

Dynamic model in LinkTo component in Ember

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.

Upvotes: 0

Views: 592

Answers (1)

NullVoxPopuli
NullVoxPopuli

Reputation: 65103

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

Upvotes: 3

Related Questions