2014-10-29 179 views
1

我有几个组件,每个组件都有一个模板,我如何为余烬组件分配一个通用模板?如何为余烬组件分配一个通用模板

+0

在这种情况下,为什么你有几部分组成?你可以有一个组件。对 ?你可以给你的代码提供你想要做的jsbin吗? – Susai 2014-10-29 08:10:46

+0

嗯,通过指定相同的模板名称? – 2014-10-29 08:12:28

+0

@Susai看起来很奇怪,但我愿意接受,可能会出现这样的情况:两个逻辑方面不同的组件可能在某种程度上有用地共享相同的模板。 – 2014-10-29 08:14:40

回答

1

要使组件之间公共的模板我可能会执行下列操作中的一个,

  1. 继承从另一个,或从一个基地一个部件延长layoutName属性

例1: http://emberjs.jsbin.com/jewohekiwu/1/edit?html,js

  • 使用共同partial辅助
  • 例2:http://emberjs.jsbin.com/helefevome/1/edit?html,js

    还具有继承的制品没有layoutName属性和一个公共部分http://blog.yanted.com/2014/01/17/inheritance-with-ember-js-components/

    例1 HBS

    <script type="text/x-handlebars" data-template-name="index"> 
        {{my-comp1 testProp="lala"}} 
        <br/> 
        {{!other-name-for-comp2 testProp="lolo2"}} 
        {{my-comp2 testProp="lolo"}} 
        </script> 
        <script type="text/x-handlebars" data-template-name="components/my-comp1"> 
        comp1 - testProp:{{testProp}} 
        </script> 
    

    JS

    App.MyComp1Component = Em.Component.extend({ 
        layoutName:"components/my-comp1", 
        test:function(){console.log("comp1");console.log(this.get("testProp"));}.on("init") 
    }); 
    
    
    App.MyComp2Component = App.MyComp1Component.extend({ 
        test:function(){console.log("comp2");console.log(this.get("testProp"));}.on("init") 
    }); 
    
    //Em.Handlebars.helper("other-name-for-comp2", App.MyComp2Component); 
    

    例题 HBS

    <script type="text/x-handlebars" data-template-name="index"> 
        {{my-comp1 testProp="lala"}} 
        <br/> 
        {{my-comp2 testProp="lolo"}} 
        </script> 
        <script type="text/x-handlebars" data-template-name="components/my-comp1"> 
    {{partial "_commonTemplate"}} 
        </script> 
        <script type="text/x-handlebars" data-template-name="components/my-comp2"> 
        {{partial "_commonTemplate"}} 
        </script> 
    
        <script type="text/x-handlebars" data-template-name="_commonTemplate"> 
        comp1 - testProp:{{testProp}} 
        </script> 
    

    JS

    App.MyComp1Component = Em.Component.extend({ 
        test:function(){console.log("comp1");console.log(this.get("testProp"));}.on("init") 
    }); 
    
    
    App.MyComp2Component = Em.Component.extend({ 
        test:function(){console.log("comp2");console.log(this.get("testProp"));}.on("init") 
    }); 
    
    +1

    第一种方法是为我工作。但是我的组件是相互独立的,所以我通过js文件中的layoutName属性为每个组件指定了一个通用模板。 – wupeng 2014-10-30 02:35:39