Ember模板链接

时间:2013-10-08 12:25:02

标签: javascript html5 ember.js handlebars.js

我有这个HTML / Handlebars代码,我想要的是“outlet fav”渲染 ID为“fav”的模板及其对应的outlet / templates-chain,带有预期的“index”。有点像显示不同的Ember URL作为索引的一部分。是否可能是原生的(没有补充iframe,我认为这是一个坏主意)?它会被认为是一种好习惯(我知道使用视图可能会有所帮助,但只是想知道走这条路是否可行)?

<script type="text/x-handlebars" id="application">
    Here we have our application
    {{outlet}}
</script>

<script type="text/x-handlebars" id="index">
    Application's First Child
    {{outlet}}
</script>

<script type="text/x-handlebars" id="index/index">
    Your Favourites
    {{outlet fav}}
</script>

<script type="text/x-handlebars" id="fav">
    {{#link-to 'fav'}}All{{/link-to}}       <!-- Link A-->
    {{#link-to 'fav.top'}}Top{{/link-to}}   <!-- Link B-->

    <!-- Expected Output from "fav/index" -->
    {{outlet}}
</script>

<script type="text/x-handlebars" id="fav/index">
     All Favourites Content
</script>

<script type="text/x-handlebars" id="fav/top">
    Top Favourites Content
</script>

<script type="text/x-handlebars" id="football">
    Football Content
</script>

JS代码: 这是我试过的东西

App.Router.map(function(){
    this.resource('index', {path: '/'}, function(){
        this.resource('fav', function(){
            this.route('top');
            this.route('last');
        });
    });
    this.route('football');
});
App.IndexIndexRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render();
        this.render("fav", {into: 'index'});
    }
});
Output:
===================================
Here we have our application
Application's First Child
Your Favourites
Link A
Link B
===================================

Omits the child outlet which should display content from "fav/index"

Help Appreciated.

1 个答案:

答案 0 :(得分:1)

您可以在IndexIndexRoute中添加额外的渲染调用,

App.IndexIndexRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render();
        this.render("fav", {into: 'index'});
        this.render("fav/index", {into: 'fav'});
    }
});

http://jsfiddle.net/7b8HT/

此外,您当然也可以使用视图来获得类似的结果。