Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify an alternative directory for my HandlebarsJS templates with the ember-rails gem?

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!

like image 559
doublea Avatar asked Feb 19 '13 01:02

doublea


1 Answers

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.

like image 90
ahmacleod Avatar answered Sep 21 '22 03:09

ahmacleod