简体   繁体   English

如何在Ember.js中创建一个子页面?

[英]How to make a subpage in Ember.js?

I have a website with one template: 我有一个带有一个模板的网站:

<script type="text/x-handlebars">
mainsite 
    {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="mainpage"> 
    mainpage
    {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="subpage"> 
    subpage
</script>

{{#linkTo 'mainpage.subpage'}}{{/linkTo}}

So what i want to acomplish is that person types address /mainpage it will see "mainsite mainpage" and when he types /mainpage/subpage he sees the "mainsite mainpage subpage". 所以我想要实现的是人类型地址/主页它会看到“主页主页”,当他输入/主页/子页面时他会看到“主页主页子页面”。

So basically i would like to have a subpage put into mainpage template and this combained into the main template (that is first on the list above). 所以基本上我想将一个子页面放入主页模板中,并将其合并到主模板中(这是上面列表中的第一个)。

I've tried: 我试过了:

this.resource('mainpage', function() {
    this.route('subpage', { path: '/subpage' });
  });

But than if i click the link created by linkTo address is set to mainpage/subpage (so this is ok), but i see "mainsite mainpage" but dont see "mainsite mainpage subpage". 但是,如果我点击linkTo地址创建的链接设置为mainpage / subpage(所以这没关系),但我看到“mainsite主页”,但没有看到“mainsite主页子页”。

What i'm doing wrong? 我做错了什么? How to fix it? 怎么解决?

I've just written a tutorial about routing and rendering views that answers this question: http://matthewlehner.net/ember-js-routing-and-views/ 我刚刚编写了一个关于路由和渲染视图的教程来回答这个问题: http//matthewlehner.net/ember-js-routing-and-views/

However, to be more specific this question, the issue here is defining route vs. resource . 但是,更具体地说这个问题,这里的问题是定义routeresource There are two options to fix your problem quickly: 有两种方法可以快速解决您的问题:

Change the route type 更改路线类型

this.resource('mainpage', function() {
  this.resource('subpage', { path: '/subpage' });
});

Change the template name 更改模板名称

data-template-name="mainpage/subpage"

This is happening because adding a 'subpage' route to a resource adds another action to this resource. 发生这种情况是因为向资源添加“子页面”路由会向此资源添加另一个操作。 In your original case, it will look for a data-template-name="mainpage/subpage" . 在原始情况下,它将查找data-template-name="mainpage/subpage"

Think of the difference between these two routes 'users/new' and 'users/1/comments' - the first should just render a new action for users, but the second is a nested resource that you'd assume would show the comments from a specific user. 想想这两条路线'users / new'和'users / 1 / comments'之间的区别 - 第一条应该只是为用户呈现一个新动作,但第二条是一个嵌套资源,你假设它会显示来自特定用户。 Using Ember, you'd make these two different routes as follows: 使用Ember,你可以按如下方式制作这两条不同的路线:

this.resource('users', function () {
  this.route('new'); #renders the 'users/new' template
  this.resource('comments'); #renders the 'comments' template
});

As an added bonus, the route names should just work. 作为额外的奖励,路线名称应该正常工作。 At least as of 1.0rc6 they do. 至少从1.0rc6开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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