2014-09-29 35 views
0

我创建了我的第一个组件,但大多数教程都假设我没有使用ember-rails。在ember-rails中创建组件时的命名约定

据我所知,一个组件需要扩展Ember.Components,也有它自己的模板,都需要正确命名,然后它可以在把手内部使用并放置在任何模板中。

我哪里错了?

# app/assets/javascripts/components/table-of-contents.js.coffee 
App.TableOfContentsComponent = Ember.Component.extend 

# app/assets/javascripts/templates/components/table-of-contents.js.hbs 
<h2>Look Ma! My component works! 
<ul> 
    <li>Item 1</li> 
    <li>Item 2</li> 
    <li>Item 3</li> 
</ul> 

# app/assets/javascripts/templates/somepage.js.hbs 
<h1>Here be Some Page with it's own Table of Contents</h2> 
{{table-of-contents}} 

该控制台给我这个荒谬的错误后,我包括{表的,内容{}}在SomePage的模板,我尝试打开SomePage的

Uncaught Error: Assertion Failed: You must pass a view to the #view helper, not function() { 

[编辑1:找到更多info内的信息。 Daah。其实没有预料到它会有更多的信息。现在通过:https://github.com/emberjs/ember-rails]

回答

0

按照设计,您的组件实际上并不需要任何自定义JavaScript。我不确定ember-rails是如何编译你的单个源文件的,但是你的组件的html源代码应该是这样的。

<script type="text/x-handlebars" id="index"> 
    <h1>Here be Some Page with it's own Table of Contents</h2> 
    {{table-of-contents}} 
</script> 

<script type="text/x-handlebars" id="components/table-of-contents"> 
    <h2>Look Ma! My component works! 
    <ul> 
     <li>Item 1</li> 
     <li>Item 2</li> 
     <li>Item 3</li> 
    </ul> 
</script> 

烬时遇到未知车把帮手,它会检查,看看是否有它定义为“组件/”模板, - 如果有的话 - 使用了。