6

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.

Community
  • 1
  • 1
Alexandros K
  • 947
  • 1
  • 6
  • 19

4 Answers4

7

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.

Alexandros K
  • 947
  • 1
  • 6
  • 19
1

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.

Drew P
  • 21
  • 3
0

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.

brg
  • 3,915
  • 8
  • 37
  • 66
  • 1
    It looks like they're manually defining every view, which is exactly the situation you want to avoid: i.e. templateName: 'ember_cart/templates/carts/mini_cart_list' – Alexandros K Jun 18 '13 at 20:51
  • @alex did the code you pasted resolve it for you. If it did, you might want to accept your own answer, so that in the future, anyone who Google brings here, will know what worked. – brg Jun 18 '13 at 21:04
  • yes, that solution definitely worked, and it seems to be a relatively stable approach. However I have to wait 2 days to accept my own answer. – Alexandros K Jun 19 '13 at 16:26
  • Thanks for looking into this as well. I hope it's helpful to you. – Alexandros K Jun 19 '13 at 20:23
0

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

noz
  • 129
  • 1
  • 5