本文介绍了了解Ember路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的路由图:

  App.Router.map  - > 
@resource'posts', - >
@resource'post',{path:'/:post_id'}

所以我在 / posts 路线和 posts / 2 之间的单个帖子列表。默认情况下,帖子模板将呈现到父模板的 {{outlet}} (应用程序)可以和发布模板到 {{outlet}} 帖子模板不是我想要的。我想用一个帖子替换帖子列表。



所以我用这个解决方案来设置所需的模板来呈现到 {{outlet}} :

  App.PostRoute = Em.Route.extend 
renderTemplate: - >
this.render(into:'application')

但结果我有列表的帖子和单个帖子呈现为一个 {{outlet}} 。我可以emtpy {{outlet}} 每个新的路线,但后来它打破按钮,cuz Ember不会再次渲染以前的模板,假设已经呈现。



所以问题是如何使用单个 {{outlet}} 为不同的路由,不要打破ember的逻辑。当然,我可以避免嵌套的路由,但是当您需要这样的语义链接时,它们真的很有用。

解决方案

如果你的模板不是嵌套的,你不应该嵌套你的路线,否则你会结束与Ember的战斗。作为Ember的州长,路线描述了您的应用程序结构。嵌套应该遵循您的用户界面,而不是您希望网址如何。



如果您关心的是URL,您可以修改路径以满足您的需求:

  App.Router.map  - > 
@resource'posts'
@resource'post',{path:'posts /:post_id'}


Assume I have routemap like this:

App.Router.map ->
    @resource 'posts', ->
        @resource 'post', {path: '/:post_id'}

So I have list of posts on /posts route and a single post on posts/2. By default posts template will be rendered to {{outlet}} of a parent template (application) which is OK and post template to {{outlet}} of posts template which is not what I want. I want to replace list of posts with a single post.

So I came with that solution to set desired template to render to it's {{outlet}}:

App.PostRoute = Em.Route.extend
    renderTemplate: ->
        this.render(into: 'application')

But as a result I have list of posts and a single post rendered into one {{outlet}}. I can emtpy {{outlet}} each new route but then it breaks back button, cuz Ember wont render previous template again, assuming that is already rendered.

So questions is how to use single {{outlet}} for different routes and don't break ember's logic. Of course I can avoid nested routes but they a really usefull when you need semantic links like this post/2/comment/12/edit.

解决方案

If your templates are not nested, you shouldn't nest your routes, or you'll end up fighting Ember. Acting as Ember's state manager, routes describe your app structure. Nesting should follow your UI, not how you want your URLs to look like.

If your concern is URLs, you can just modify the path to fit your needs:

App.Router.map ->
  @resource 'posts'
  @resource 'post', { path: 'posts/:post_id' }

这篇关于了解Ember路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:15