2013-07-29 33 views
1

我的代码:http://jsbin.com/axaqix/20/editEmber.js - 呈现模板嵌套资源和模板覆盖

我的路由器:

WebApp.Router.map(function() { 
    this.resource('sites', { path: '/' } , function() { 
     this.resource('site', { path: '/sites/:site_id'} , function() { 
      this.resource('posts', { path: 'posts' }); 

     }); 

     this.route('new', {path: 'new'}); 

    }); 

}); 

我的模板:

<!--Main Page--> 
<script type="text/x-handlebars" data-template-name="sites"> 
<ul> 
    {{#each site in controller}} 
    <li> 
     {{#linkTo 'site' site}} {{site.siteName}} {{/linkTo}}<br> 
     {{#linkTo 'posts' site}} go to posts {{/linkTo}} 
    </li> 
    {{/each}} 

</ul> 
{{#linkTo 'sites.new'}} ADD NEW WEBSITE {{/linkTo}} 

{{outlet}} 

<!--Displays site details--> 
<script type="text/x-handlebars" data-template-name="site"> 
    <p>Site Details</p> 
    Name: {{siteName}} <br> 
    Secret Key:{{secretKey}} <br> 
    Public Key:{{publicKey}} <br> 

</script> 


<!--Inseting new WEBSITE template--> 
<script type="text/x-handlebars" data-template-name="sites/new"> 
    <p>Add New WEBSITE</p> 
    <div> 
     {{view Ember.TextField placeholder="Insert new site name" 
     valueBinding="newName" action="addSite"}} 
    </div> 
</script> 


<script type="text/x-handlebars" data-template-name="posts"> 
    <p>Damn</p> 

</script> 

我h大道两个问题: 1.为什么当我按go to posts一行它不是渲染posts模板?相反,它是渲染 site模板。

  1. 是否可以让posts模板覆盖sites模板?

注意:有两个模板sitesites

回答

5
  1. 为什么当我按下转到帖子行它不是呈现帖子模板?相反,它是呈现网站模板。

您正在将帖子路线呈现为站点路线的childRoute。所以当你转换到帖子时,转换将会像“sites.site.posts”一样。因此,父模板首先被渲染,然后是孩子。

  1. 是否可以使帖子模板覆盖网站模板?

是的,你可以命名的网站模板您的插座为{{outlet test}}

和渲染网站和帖子模板到它作为

WebApp.PostsRoute = Ember.Route.extend({ 
    renderTemplate: function() { 
    this.render('posts',{ 
     into: 'sites', 
     outlet: 'test' 
    }); 
    } 
}); 

Working fiddle

更新

甚至没有必要给你的网点命名。你可以简单地使网站和帖子模板到网站模板

WebApp.PostsRoute = Ember.Route.extend({ 
renderTemplate: function() { 
    this.render('posts',{into: 'sites'}); 
} 
}); 

Updated fiddle

+0

感谢您的回答,但没有什么,我在第二个问题的意思。我想重写'sites'模板,它是显示列表的模板。因此,该死的字将显示在下面TITLE – Canttouchit

+0

只需将你的帖子模板作为this.render('posts',{into:'application'})渲染到应用程序中。 – selvagsz

+0

如果这不起作用,请恢复。 – selvagsz