How does Ember.js reference Grunt.js precompiled Handlebars templates?

这一生的挚爱 提交于 2019-12-04 02:39:58
Mike Grassotti

What I don't understand, is how Ember.js is supposed to use the precompiled templates.

Ember expects that compiled templates will be added to the Ember.TEMPLATES property. When an ember application is loaded it checks for any handlebars script tags and compiles them. Each template is then added to Ember.TEMPLATES using the specified data-template-name attribute as the key. If no data-template-name is provided they key is set to application.

Other than that ember does not care how things get into Ember.TEMPLATES. You can add/remove templates from it manually. For example, https://stackoverflow.com/a/10282231/983357 demonstrates how you can compile a template inline:

Ember.TEMPLATES['myFunkyTemplate'] = Ember.Handlebars.compile('Hello {{personName}}');

Now obviously you don't want to write your templates this way, you want grunt to do it for you, but as you can see there is nothing magic going on....

According to the Ember docs when an application loads, it will look for the application template. How does that work with index.html and by changing the file name of the template, is that changing the template's name?

Ember doesn't care about file name of the template, it just cares what string was used as key in Ember.TEMPLATES['key/goes/here']. That said, it makes a lot of sense to use filename as the key for your templates.

Could someone point me in the right direction as to how Ember.js uses precompiled templates?

I think what's missing from your project is probably that the compiled templates are not being added to Ember.TEMPLATES. AFAIK the grunt-contrib-handlebars plugin does not do this. Consider using grunt-ember-templates instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!