0

我正在创建我的第一个组件,但大多数教程都假设我没有使用 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 模板中包含 {{table-of-contents}} 并尝试打开 somepage 后,控制台给了我这个无意义的错误

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

[编辑 1:在 gem README 中找到更多信息。啊。实际上没想到它会有更多关于此的信息。现在通过:https ://github.com/emberjs/ember-rails ]

4

1 回答 1

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>

当 ember 遇到未知的车把助手时,它会检查您是否将其定义为“components/”中的模板,如果是,则使用它。

于 2014-10-02T18:53:49.607 回答