简体   繁体   English

Ember.js-在某些模板中加载其他.js文件

[英]Ember.js - Loading additional .js-Files in some templates

Im developing a ember.js based app. 我正在开发一个基于ember.js的应用程序。 On some "sites" (templates) I want to load a specific js-game, so I have to include extra tags like <script src="game.js"></script> . 在某些“站点”(模板)上,我想加载特定的js游戏,因此我必须包括<script src="game.js"></script>类的额外标签。 But since handlebar-templates are defined by <script> -Tags itself, its not possible to simply put my dependencies within a template directly. 但是由于把手模板是由<script> -Tags本身定义的,因此不可能简单地将我的依赖项直接放在模板中。

How can I include js-files on some individual sites only? 如何仅在某些单独的网站上包含js文件?

Including files in an Ember.js application is a bit more complex than in a regular website. 在Ember.js应用程序中包含文件比在常规网站中复杂一些。

If you do not use ember-cli , then you could either include your all your JavaScript files directly in your index.html (one by one) or (and this is better) you could also bundle all your game JavaScript files into a single file (called games.js for instance) and include that single file in your index.html . 如果您使用ember-cli ,则可以将所有JavaScript文件直接(一个一个地)包含在index.html ,或者(这样更好),您还可以将所有游戏JavaScript文件捆绑到一个文件中(例如称为games.js ),并将该文件包含在index.html You can bundle JavaScript files using tools such as grunt or brunch or broccoli . 您可以使用grunt早午餐西兰花之类的工具捆绑JavaScript文件。

Now if you do use ember-cli (which I recommend), then you could simply list your files in your Brocfile.js (see documentation here ). 现在,如果您确实使用ember-cli(我建议这样做 ),那么您只需在Brocfile.js中列出文件(请参阅此处的文档)。 Learning ember-cli might take a little bit of extra time but it will really help you in the future :) 学习ember-cli可能会花费一些额外的时间,但将来确实会对您有所帮助:)

Good luck! 祝好运!

Ok I found an possibility to solve that problem: 好的,我发现了解决该问题的可能性:

Like described in the handlebars.js-FAQ here (5.), I have to use some kind of a "Hack" to avoid parsing errors. 就像这里的handlebars.js-FAQ(5。)中所述,我必须使用某种“ Hack”来避免解析错误。 Just need to add an empty command {{!}} into the word "script" like <scr{{!}}ipt src=...>...</scr{{!}}ipt . 只需在单词“ script”中添加一个空命令{{!}} ,例如<scr{{!}}ipt src=...>...</scr{{!}}ipt

That works for me. 这对我行得通。 Also, as kpdecker says here , it is better to use precompiled templates than defining them inline. 而且,正如kpdecker 在这里说的那样,使用预编译的模板比内联定义它们更好。

You can try to insert the necessary scripts from didInsertElement hook of the corresponding view. 您可以尝试从相应视图的didInsertElement挂钩中插入必要的脚本。 And, if so, in order to avoid duplicates, remove that scripts in willDestroyElement hook of the same view. 并且,如果是这样,为了避免重复,请在同一视图的willDestroyElement挂钩中删除该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM