6
votes

Ember & Rails newb here with a serious question about both.

I'm building a project that leverages the ember-rails setup as exemplified here: https://github.com/dgeb/ember_data_example

However, to make things even more complicated, the project is intended to be packaged as a gem, for inclusion in a larger app. To that end I've created a mountable engine, containing a dummy project for testing.

Problem is, ember-rails, and and specifically handlebars cannot find the templates in their intended location. This prevents a lot of the functionality from working properly.

In terms of moving the handlebars template directory, I have found a solution here: How can I specify an alternative directory for my HandlebarsJS templates with the ember-rails gem?

In summary it states we can specify the path to the templates in application.rb by setting the value of config.handlebars.templates_root

However, I think this will cause a conflict should another ember-rails engine need to be loaded into a larger app.. and that one will need its own configuration setting.

Does anyone have experience with these setups, and is there any way to make a namespaced ember app play nice within a larger context?

By the way, this is rails 4 running the latest ember-rails on ruby 1.9.2.

4

4 Answers

7
votes

After doing a lot of research, it seems that Ember supports overriding the Default Resolver: http://emberjs.com/api/classes/Ember.DefaultResolver.html

You can instruct it to look for templates in your namespaced application by following the instructions here: https://github.com/emberjs/ember.js/pull/2354

In essence, a resolver can be added to your Ember.Application.create(): (to quote @lukemelia in the aforementioned pull request)

App1 = Ember.Application.create({
    resolver: Ember.DefaultResolver.extend({
        resolveTemplate: function(parsedName) {
          parsedName.fullNameWithoutType = "app1/" + parsedName.fullNameWithoutType;
          return this._super(parsedName);
        }
    })
});

This seems to be a part of ember rc5 at the time of writing.

1
votes

I just created an ember app inside a rails engine and I had the same issues as you did w/ getting handlebars support. I couldn't get ember-rails to work inside an engine so I looked at ember-rails source and I ended up just registering handlebars w/ tilt in my engine directly. There are some configurations inside ember-rails to change the root template path: handlebars.templates_root. However I didn't have success in getting that to work inside a rails engine.

https://github.com/andrewmp1/spree_outlet

I still haven't really grokked how I would do integration tests w/ the dummy app. But its coming along.

0
votes

This an old ember app and also a rails engine. It uses ember-rails gem based on the gemfile entry. You may want to look it up. See it mounted in a rails app here: ember-cart-example. Also, here is the online demo of the mounted rails engine inside the rails app.

0
votes

There is another solution for this: isn’t very efficient but it’s simple and it will work for this case (ember as engine inside rails app). Start by creating an initializer in your engine. There’s no initializers directory in the engine’s config directory but you can create one and initializers placed in it will work.

# path_to_engine/config/initializers/ember.rb
YourEngine::Engine.config.handlebars.templates_root = "your_path/templates"

Any other ember-specific configuration related to the engine could be also placed here