2014-09-29 98 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> 

燼時遇到未知車把幫手,它會檢查,看看是否有它定義爲「組件/」模板, - 如果有的話 - 使用了。