如何在 Ember 中将一个视图用于多个路由

How to use one view for multiple routes in Ember

我在 views/posts/index.js 有一个观点。我有一条路线用于帖子列表,另一条路线用于搜索引擎。第二条路是搜索引擎怎么用?

假设你有一个模板posts

<script type="text/x-handlebars" id="posts">
  <h3>{{ route }}</h3>
</script>

您想要同时用于 posts/indexposts/search

您可以像这样在路由中覆盖 renderTemplate 方法:

App.PostsIndexRoute = Ember.Route.extend({
  model: function(){
    return { route: "INDEX"};
  },
  renderTemplate: function(controller, model) {
    this.render('posts', {
      into: 'application',
      controller: controller,
      model: model
    }); 
  }
});

App.PostsSearchRoute = Ember.Route.extend({
  model: function(){
    return { route: "SEARCH"};
  },
  renderTemplate: function(controller, model) {
    this.render('posts', {
      into: 'application',
      controller: controller,
      model: model
    }); 
  }
});

工作演示here