首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ember-rails中创建组件时的命名约定

在ember-rails中创建组件时的命名约定
EN

Stack Overflow用户
提问于 2014-09-29 09:00:06
回答 1查看 305关注 0票数 0

我正在创建我的第一个组件,但大多数教程都假定我没有使用ember-rails。

据我所知,组件需要扩展Ember.Components,还需要有自己的模板,两者都需要正确命名,然后才能在handlebars中使用,并放置在任何模板中。

我哪里错了?

代码语言:javascript
复制
# 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-content}}并尝试打开somepage时,控制台显示此无意义的错误

代码语言:javascript
复制
Uncaught Error: Assertion Failed: You must pass a view to the #view helper, not function () {

[编辑1:在gem自述文件中找到更多信息。啊哈。实际上没有想到它会有更多关于这方面的信息。正在处理中:https://github.com/emberjs/ember-rails ]

EN

回答 1

Stack Overflow用户

发布于 2014-10-03 02:53:50

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

代码语言:javascript
复制
<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遇到未知的handlebars帮助器时,它会检查您是否将其定义为"components/“中的模板,如果是,则使用该模板。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26091194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档