由于我们使用yeoman / grunt,我遇到了this handlebars plugin.我已将其配置如下:
handlebars: { compile: { options: { namespace: 'JST' },files: { '<%= yeoman.dist %>/scripts/templates.js': [ '<%= yeoman.app %>/templates/*.hbs' ],} } }
正如插件的“用法示例”部分所示.这正在按预期工作,生成dist / scripts / templates.js文件.但是将模板放在’JST’命名空间中使得它们不再可以访问Ember.这是我得到的错误:
Uncaught Error: assertion Failed: You specified the templateName application for <App.ApplicationView:ember312>,but it did not exist.
一些问题:
>我如何“初始化”现在在templates.js中的把手模板,以便Ember可以找到它们?
>我如何告诉ember将模板放在DOM中的位置,因为模板现在不在文档正文中?
这是我的index.html:
<!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <Meta charset="utf-8"/> <Meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <title></title> <Meta name="description" content=""/> <Meta name="viewport" content="width=device-width"/> <!-- build:css styles/main.css --> <link rel="stylesheet" href="styles/bootstrap.min.css"/> <link rel="stylesheet" href="styles/css/font-awesome.min.css"/> <link rel="stylesheet" href="styles/font-styles.css"/> <link rel="stylesheet" href="styles/main.css"/> <!-- endbuild --> </head> <body> <!--[if lt IE 7]> <p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p> <![endif]--> <!-- Add your site or application content here --> <!-- My templates used to be here,but now they are in templates.js --> <div class="pagination"> <ul> <li><a href="#">Prev</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">Next</a></li> </ul> </div> </script> <!-- build:js scripts/scripts.js --> <script src="scripts/vendor/jquery-1.9.1.js"></script> <script src="scripts/vendor/handlebars.1.0.rc.3.js"></script> <script src="scripts/vendor/ember-1.0.0-rc.2.js"></script> <script src="scripts/vendor/ember-data.js"></script> <script src="scripts/vendor/bootstrap.min.js"></script> <script src="scripts/templates.js"></script> <script src="scripts/main.js"></script> <!-- endbuild --> </body> </html>
解决方法
Since we are using yeoman/grunt,I have come across this handlebars plugin.
这就是绊倒你的原因.您正在尝试使用grunt-contrib-handlebars插件,因此编译的模板未添加到Ember.TEMPLATES.有可能配置grunt-contrib-handlebars来实现这一点,但它可能更容易使用(grunt-ember-templates)[https://github.com/dgeb/grunt-ember-templates]而不是.
有关详细信息,请参见How does Ember.js reference Grunt.js precompiled Handlebars templates?
How can I “initialize” the handlebars templates,which are right nor in templates.js,so that Ember can find them?
Ember并不关心事情如何进入Ember.TEMPLATES.您可以手动添加/删除模板.例如:
Ember.TEMPLATES['myFunkyTemplate'] = Ember.Handlebars.compile('Hello {{personName}}');
How can I tell ember where to place the templates in the DOM,since the templates are now out of the document body?
模板的来源并不重要,所有的ember看起来都是模板的id. application.hbs模板将呈现到应用程序的根元素(默认为document.body),所有其他模板通过视图助手等呈现到出口中.