i'm developing ember addon and i need to share one route between all applications that use this addon but i don't know how to achieve that or if it's even possible. Already i've add routes in addon, but application that counsume addon, doesn't see this. Any suggestions? Thanks!
2 Answers
Yes it is possible. What you put in your addon's app directory will be merged with consuming applications' app directory. (Resource) Put your routes into the correct directories.
On the other hand, you need to add your routes to the router mapping. You can use instance-initializers for this purpose. Also doesn't forget to put your instance-initializers in your app directory. All you need to do in your initializer function is calling Router.map(...)
The code will be something like that:
import Router from '../router';
function initialize(){
Router.map(function() {
this.route('yourroute');
});
}
ps: code updated

- 6,139
- 3
- 29
- 56
-
Hi, thank you for reply. But how can i access Router object in initializer? – Michał Marcinkowski Sep 02 '16 at 09:42
-
Router object exported by app/router.js. Code was updated. – ykaragol Sep 02 '16 at 11:09
-
Note that: It s not a must for you to do it, addon users can do this. But if you want to force it, or if you want to prevent missing by users; you can use this way. – ykaragol Sep 02 '16 at 11:10
-
I am kind of in the same situation. I can get my route working but it says there is no template to render it. Is there any specific way to add templates? – Aditya Kappagantula Sep 16 '16 at 01:45
-
By convention, route templates' are placed under `app/template`. It must have the same name with route. But you can change the default template as described [in here](https://guides.emberjs.com/v2.8.0/routing/rendering-a-template/) with `templateName` or `renderTemplate()` – ykaragol Sep 16 '16 at 05:32
-
This answer got me most of the way towards accomplishing this same goal. However, there was one issue I ran into. In an addon, there is no `Router` to import. Instead, I had to do `function initialize(application){ let router = application.__container__.lookupFactory('router:main'); router.map(function() { this.route('yourroute'); }); }` – Taylor Hobbs Oct 03 '16 at 14:21
-
Try this: `let appInstance = Ember.getOwner(this); let router = appInstance.lookup('router:main');` – ykaragol Oct 03 '16 at 16:55
In an Ember Engines RFC discussion, bcardarella, asks, "What's the difference between an Ember Engine and Ember Addon"? The discussion continues where ef4 says, "The only thing addons can't do is add routes to the router map.", (I think he means in an automatically merged way), however an alternative and example is given in the RFC thread:
https://github.com/emberjs/rfcs/pull/10#issuecomment-60504581 https://github.com/dockyard/ember-admin#usage
Thus you can use the workaround approach suggested by bcardarella. Or, you can try Ember Engines (which is also delivered in an Addon package).

- 1,547
- 1
- 19
- 27