Ember-cli是什么?

32

我过去一个月一直在使用Yeoman的Ember生成器,现在,我想尝试一下ember-cli。

我运行了生成器并启动了应用程序,一切都很正常。

ember new my-new-app
ember server
但我想知道的是,它是如何运作的。
{{content-for 'head'}}

在app/index.html中起作用吗?

当查看其他来自http://www.ember-cli.com/#tutorials的示例时,它们没有使用此特定助手?是因为它们使用较旧版本的ember-cli吗?为什么他们不使用这个content-for助手?

我很确定ember.js默认没有这个content-for助手,所以我猜测ember-cli在某个地方编写了它?它在哪里以及它的作用是什么?

此外,当我检查my-new-app页面的元素时,“欢迎使用Ember.js”的div标记出现在body标记而不是head标记中?这怎么可能? {{mind-blown}}

(在app/index.html中,{{content-for 'head'}}放置在head标记内)

2个回答

26
最近根据这个讨论,它被添加到了基于ember-cli的项目中。
以下是提交记录中相关的代码:
EmberApp.prototype.contentFor = function(config, match, type) {
  var content = [];

  if (type === 'head') {
    content.push(calculateBaseTag(config));

    content.push('<meta name="' + config.modulePrefix + '/config/environment" ' +
                 'content="' + escape(JSON.stringify(config)) + '">');
  }

  content = this.project.addons.reduce(function(content, addon) {
    if (addon.contentFor) {
      return content.concat(addon.contentFor(type, config));
    }

    return content;
  }, content);

  return content.join('\n');
};

2
有关更多上下文,请参见https://www.npmjs.org/package/ember-cli-inline-content。 - aceofspades

21

来自Ember CLI指南:

app/index.html

app/index.html文件为您的应用程序奠定了基础。这是布置基本DOM结构、设置标题属性以及包含样式表/JavaScript的地方。除此之外,app/index.html还包括多个钩子 - {{content-for'head'}}{{content-for 'body'}} - 可供插件使用,将内容注入到您的应用程序的headbody中。这些钩子需要保留在原位,以使您的应用程序正常运行,但是除非您直接使用特定插件,否则可以安全地忽略它们。

我实际上正在寻找Welcome to Ember.js源代码的位置(显然是在app\templates\application.hbs中),但是我先碰到了content-for助手。


即使在插件中,您如何将代码注入到头部? - Epirocks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接