How do I retrieve the path of a route?
Asked Answered
S

1

4

Given the following Router setup:

App.Router.map(function() {
  this.resource('posts', function() {
    this.route('new');
    this.resource('post', { path: ':post_id' }, function() {
      this.route('edit');
    });
  });
});

Is it possible to obtain the path of a route?

Fictitious example:

App.Router.get('path', 'posts.new'); //=> "/posts/new"

Or with a model like:

App.Router.get('path', 'post.edit', model); //=> "/posts/1/edit"
Shellashellac answered 9/5, 2013 at 21:2 Comment(1)
Maybe you wanna take a look into the source. The LinkView implements thatCountess
F
4

Try this in your console/dev tools:

App.Router.router.generate(['posts.new']);

this should output:

/posts/new

And with a model:

App.Router.router.generate(['post.edit'], postModel);

will output

/posts/1/edit

Thanks to @MilkyWayJoe's reference! https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/helpers/link_to.js#L112-L117 :)

Financial answered 9/5, 2013 at 21:36 Comment(3)
Just keep in mind this might not generate urls for dynamic routes without the actual object (it has nothing to serialize!).Countess
Yes, that works! And if you want to add a model, just pass it as a second parameter. App.Router.router.generate(['post.edit'], postModel); Thanks guys =]Shellashellac
Where is the documentation for this? I can't find it.Stealthy

© 2022 - 2024 — McMap. All rights reserved.