如何使用ember-rails gem为HandlebarsJS模板指定备用目录? [英] How can I specify an alternative directory for my HandlebarsJS templates with the ember-rails gem?

查看:194
本文介绍了如何使用ember-rails gem为HandlebarsJS模板指定备用目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,而我正在前端使用Ember。我想将ember相关的文件移动到目录结构中的一个级别,但是当我这样做时,模板不再呈现。



在平原中,香草,应用程序的工作版本,我的目录结构是:

  ./ app / 
assets /
javascripts
application.js
ember-app.js
routes.js
store.js
models /
controllers /
routes /
templates /
views /






with:application.js

  // = require jquery 
// = require jquery_ujs
// = require handlebars
// = require ember
// = require ember-data
// = require_self
// = require ember-app
App = Ember。 Application.create();






和:ember-app.js

  // = require ./store 
// = require_tree ./models
// = require_tree ./controllers
// = require_tree ./views
// = require_tree ./helpers
// = require_tree ./templates
// = require ./router
// = require_tree ./routes

一切正常。但是,我想将ember应用程序文件和所有ember JavaScript代码移动到一个级别,当我这样做时,模板不会呈现。 (应用程序的一部分使用Ember,但不是整个应用程序,我正在设法通过资产管道设置两个单独的路径。)



所需的结构是:

  ./ app / 
assets /
javascripts
application.js
embro /
ember-app.js
routes.js
store.js
models /
controllers /
routes /
templates /
views /






with:application。 js(修改:'require ember-app'变成'需要embro / ember-app')

  // = require jquery 
// = require jquery_ujs
// = require handlebars
// = require ember
// = require ember-data
// = require_self
/ / = require embro / ember-app
App = Ember.Application.create();

(ember-app.js未修改。)



正如我所说,移动之后,模板内容都不会出现在屏幕上。屏幕上或控制台中没有错误,只是一个空的ember应用程序。



当我在控制台中检查Ember.TEMPLATES时,列出了所有预期的模板。此外,如果我将所需的内容放置在相应rails视图中的x-handlebars模板中,则内容成功呈现,就像原始目录结构一样。



例如,在apps / views / welcome / index.html ....

 < script type =text / x-handlebarsdata-template-name =application> 
< h1> hello< / h1>
{{outlet}}
< / script>

< script type =text / x-handlebarsdata-template-name =index>
< h1>这是索引< / h1>
< / script>

...我们很乐意再去。



但是如果我将轨道视图留空,就像我用原来的结构一样,这是一个没有去的。



想知道是否还有ember-rails gem需要将handlebars模板存在于app / assets / javascripts / templates中,如果有办法覆盖这个。该文档提到在应用程序配置块中添加一个templates_root选项,我想知道这是否是关键。我已经玩了一点,没有运气。



任何想法?






更新:



害怕我没有运气与templates_root选项。作为一个实验,我尝试构建一个新的,简单的rails应用程序,并使用ember-rails引导生成器来获取它并运行。一切顺利,但是如果我尝试简单地更改模板文件夹(即app / assets / javascripts / templates - > app / assets / javascripts / temple)的名称,可以对链轮进行适当的更改包括和配置文件,我得到相同的结果。



任何机会,templates_root选项都会破碎?



我使用的是Ruby 1.9.3,Rails 3.2 .11,ember-rails 0.10.0



任何指向我应该查看ember / ember-rails / handlebars源代码的指针?已经开始戳戳了。



谢谢!

解决方案

你需要设置 templates_root 。尝试添加

  config.handlebars.templates_root ='embro / templates'
pre>

application.rb 中的配置块,或

  RailsApp :: Application.config.handlebars.templates_root ='embro / templates /'

到一个新的初始化程序,其中 RailsApp 是您的应用程序命名的。



编辑:



我能够重现您用 templates_root 描述的行为。我的修复是删除我的应用程序的 / tmp 文件夹,并重新启动rails。之后,模板被正确命名。



编辑:



更改 templates_root 之后,您需要清除 / tmp / cache / assets 之间的链轮缓存。



编辑:



如下面的评论所述,一个简单的 tmp:cache:clear 应该处理问题。


I have a Rails application, and I'm using Ember on the front-end. I'd like to move the ember-related files down one level in the directory structure, but when I do, the templates no longer render.

In the plain, vanilla, working version of the application, my directory structure is:

./app/
  assets/
    javascripts
      application.js
      ember-app.js
      routes.js
      store.js
      models/
      controllers/
      routes/
      templates/
      views/


with: application.js

//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require ember-app
App = Ember.Application.create();


and: ember-app.js

//= require ./store
//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./views
//= require_tree ./helpers
//= require_tree ./templates
//= require ./router
//= require_tree ./routes

Everything works fine. However, I would like to move the ember-app file and all ember javascript code down one level, and when I do so, the templates do not render. (Part of the application uses Ember, but not the entire application, and I'm trying to set up two separate paths through the asset pipeline.)

The desired structure is:

./app/
  assets/
    javascripts
      application.js
      embro/
        ember-app.js
        routes.js
        store.js
        models/
        controllers/
        routes/
        templates/
        views/


with: application.js (revised: 'require ember-app' becomes 'require embro/ember-app')

//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require embro/ember-app
App = Ember.Application.create();

(ember-app.js is unrevised.)

As I said, after the move, none of the template content appears onscreen. No errors onscreen or in the console, just an empty ember-application.

When I examine Ember.TEMPLATES in the console, all the expected templates are listed. Furthermore, if I put the desired content in x-handlebars templates in the appropriate rails view, the content successfully renders, just as it did with the original directory structure.

For example, in apps/views/welcome/index.html....

<script type="text/x-handlebars" data-template-name="application">
  <h1>hello</h1>
  {{ outlet }}
</script>

<script type="text/x-handlebars" data-template-name="index">
  <h1>this is the index</h1>
</script>

... and we're good to go again.

but if I leave the rails view empty, as I did with the original structure, it's a no go.

Wondering if perhaps the ember-rails gem requires the handlebars templates to be present in app/assets/javascripts/templates, and if there's a way to override this. The documentation mentions adding a templates_root option to the application configuration block, and I'm wondering if this is the key. I've played around a bit, no luck yet.

Any ideas?


UPDATE:

Afraid I'm not having any luck with the templates_root option. As an experiment, I tried building a new, simple rails app, and using the ember-rails bootstrap generator to get it up and running. All's well, but if I then attempt to simply change the name of the templates folder (i.e. app/assets/javascripts/templates -> app/assets/javascripts/temple), with appropriate changes to the sprockets includes and config files, I'm getting the same results.

Any chance the templates_root option is somehow broken?

I'm using Ruby 1.9.3, Rails 3.2.11, ember-rails 0.10.0

Any pointers to where I should look in the ember / ember-rails / handlebars source code? Have started poking around.

thanks!

解决方案

You're right that you need to set templates_root. Try adding

config.handlebars.templates_root = 'embro/templates'

to the configuration block in application.rb, or

RailsApp::Application.config.handlebars.templates_root = 'embro/templates/'

to a new initializer, where RailsApp is whatever your application is named.

Edit:

I was able to reproduce the behaviour that you described with templates_root. The fix for me was to delete the /tmp folder of my application and restart rails. After that, the templates were named correctly.

Edit:

More precisely, you need to clear the sprockets cache at /tmp/cache/assets after changing templates_root.

Edit:

As mentioned in the comments below, a simple rake tmp:cache:clear should take care of the problem.

这篇关于如何使用ember-rails gem为HandlebarsJS模板指定备用目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆