I am new to Ember and was wondering if there is a way to refer back to a URL that opens the Ember app. I would like to be able to refer to that URL from anywhere in the application without hardcoding the value. The user should be able to get back to page in which launched the Ember app. I would like to avoid using window.history.go() with the hope of there being a better way to achieve this.
You could technically store the value of window.opener.location.href
or window.document.referrer
into a service property, but you may bumb into cross domain stuff if the window that launched your app is in a different domain
The router has an access to rootURL property.
this.get(‘router.url’);
i think it has access to rootURL property as well.
https://emberjs.com/api/ember/2.18/classes/Router/methods
Best, Elton
According to docs, rootURL
is simply the root url of the app, typically ‘/’ (so not what OP was looking for I think).
The user should be able to get back to page in which launched the Ember app
Anyway I agree you’ll want to store the initial page URL in a service
But then I would populate it in app/routes/application.js
:
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default Route.extend({
router: service(),
firstPage: service(),
actions: {
didTransition() {
if(!this.get('firstPage.url')) {
this.set('firstPage.url', this.get('router.currentURL'));
}
},
}
});
Then you’d literally define an empty service in app/services/first-page.js
.
Then maybe create a component app/components/return-to-first-page.js
:
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
firstPage: service(),
router: service(),
click() {
this.get('router').transitionTo(this.get('firstPage.url'));
}
});
(little-known fact is that transitionTo
can take a url )
This approach worked me. Thanks!