1

I'm working on an Ember app that will need to be merged with another ember app with a Rails back-end (using ember-rails gem). Is it possible to have two totally separate ember apps connected by a Rails back-end? There will be some shared resources (through Rails) but otherwise the apps are very different and it would be ideal to be able to serve them both with one Rails app as opposed to making two separate Rails apps for each Ember app...

thanks

2 Answers2

1

Definitely possible. There are multiple ways to do this. My advice is to make your Ember.js apps share one namespace and instance.

Below is a code example (credit to @brg)

MainRailsApp = Ember.Application.create(); RailsEngine = Ember.Namespace.create(); RailsEngine2 = Ember.Namespace.create();

Based on this, the acceptable approach is below

App = Ember.Namespace.create({name: "App" }); Main = App.MainApp = Ember.Application.extend(); Main.create();

Either are valid and you can decide based on your application-specific needs and workflow.

Tejas Manohar
  • 954
  • 2
  • 11
  • 28
1

Yes, totally possible.

You will need a Rails route for each app. eg /app1 and /app2

I have a separate javascript manifest file for my ember app and for my Rails app, but I don't see why you can't combine both ember apps into application.js manifest file if you want to.

You can instantiate your Ember app as follows:

App = Ember.Application.create({
  rootElement: '#PUT_YOUR_ROOT_DOM_ELEMENT_HERE',
  Resolver: Ember.DefaultResolver.extend({
    resolveTemplate: function(parsedName) {
      parsedName.fullNameWithoutType = "PUT_THE_RELATIVE_RAILS_URL_FOR_YOR_EMBER_APP_HERE/" + parsedName.fullNameWithoutType;
      return this._super(parsedName);
    }
  })
});

Embers default behaviour is to attach to the bottom of the DOM, You can specify a rootElement as shown above.. Useful if you re-use header or footer from Rails app. Solution above shows how you specify your Rails url for the ember app, so that Ember urls are relative to it.

I got this solution from here: Ember-Rails and namespaced templates

UPDATE:

You should also specify the relative Rails url in the Router

App.Router.reopen({    
  rootURL: '/PUT_THE_RELATIVE_RAILS_URL_FOR_YOR_EMBER_APP_HERE'
});
Community
  • 1
  • 1
ianpetzer
  • 1,828
  • 1
  • 16
  • 21
  • +1 Nice post. It's all about the asset pipeline and rails routing. Adding a template resolver is perfect if you want to have a `common` folder that is used between apps. Have used a pattern like this several times and it's consistently a pleasure to work with. – Matthew Blancarte Jun 27 '14 at 17:59
  • Thanks! I use this solution myself – ianpetzer Jun 27 '14 at 18:01