简体   繁体   中英

Ember.js: Passing parameters to nested routes

I'm trying to build a nested route in Ember. The goal is to have the #/posts page present a list of posts, each with a link (#/posts/summary/:post_id) to nested a resource called summary.

The summary resource contains 4 nested routes, let's call them (a, b, c, d) meaning we have a route like #/posts/summary/:post_id/a. The template for route 'a' contains tags for the 'post' object with id = post_id.

How do you make the post model available to the 'a' route?

Here's my code in it's current state:

/* router.js */
App.Router.map(function(){
  this.resource('posts', function(){
    this.resource('summary', {path:'summary/:post_id'}, function(){
      this.route('a'); /* path autogen to be #/posts/summary:post_id/a */
      this.route('b');
      this.route('c');
      this.route('d');
    });
  });
});


/* controllers.js */
App.PostsIndexRoute = Ember.Route.extend({  
  model: function(){
    return this.store.find('post');
  }
});

App.SummaryRoute = Ember.Route.extend({
  model: function(params){
    return this.store.find('post', params.post_id);
  },
});

App.SummaryIndexRoute = Ember.Route.extend({
  model: function(params){
    console.log(params.post_id);
    return this.store.find('post', params.post_id);
  },
});

AdminApp.SummaryARoute = Ember.Route.extend({
  model: function(params){
    return this.store.find('post', params.post_id);
  },
});

/* 'A' template */
I'm in A route. Here's the {{title}} of the post. 

Any help would be greatly appreciated. Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM