Search code examples
routesember.jsember-router

'match' syntax for nested routes in ember


My question involves the new routing mechanism which was integrated into the master ember.js branch not too long ago.

In the following code block:

App.Router.map(function(match) {
  match("/comments").to("commentsIndex");
});

The ember router now recognizes the comments url, and maps the route handler to commentsIndexRoute via the first parameter of the "to" method (in this case "commentsIndex"). From here you can choose to use the default mappings to the views/templates/controller, or perhaps override them.

However in the following nested route:

App.Router.map(function(match) {
  match("/posts").to("posts", function(match) {
    match("/").to("postsIndex");
    match("/:post_id").to("postShow");
  });
});

I am now unsure what the role is of the first parameter for "to" method in the first match. In other words, I am unsure of the role of the capitalized parameter below:

 match("/posts").to("POSTS", function(match) {

what does that parameter do exactly?


Solution

  • It seems that this is now the recommended way of embedding "views".

    From the new Ember documentation:

    Visiting /posts is slightly different. It will first render the posts template into the application template's outlet. Then, it will render the postIndex template into the posts template's outlet.

    Finally, visiting /posts/new will first render the posts template, then render the newPost template into its outlet.

    http://emberjs.com/guides/routing/defining-your-routes/

    So, .to("POSTS") sets the context to load the following views.