In my application I need to have certain sections of my app hard refresh on transition. I have gotten pretty close by creating another Location
type I call
passthrough
and then separating out the routes into a separate file. The passthrough
location option essentially attempts to replace any where that history state was previously pushed and instead just location.href =
the new pathname
. The problem I am having now is that while this more or less works the router has already triggered a run loop and the view is rendered for a split second before it is refreshed. Not super familiar with how to control the run loop so thought maybe someone could help.
/location/passthrough.js
import Ember from 'ember';
/* Implement history API that hard refreshes on transition */
export default Ember.Object.extend({
implementation: 'passthrough',
rootURL: '/',
init() {
this.set('location', this.get('location') || window.location);
this.set('baseURL', Ember.$('base').attr('href') || '');
},
setURL(path) {
this.get('location').href = path;
},
replaceURL(path) {
this.get('location').href = path;
},
getURL() {
var rootURL = this.get('rootURL');
var location = this.get('location');
var path = location.pathname;
var baseURL = this.get('baseURL');
rootURL = rootURL.replace(/\/$/, '');
baseURL = baseURL.replace(/\/$/, '');
var url = path.replace(baseURL, '').replace(rootURL, '');
var search = location.search || '';
url += search;
url += this.getHash();
return url;
},
onUpdateURL() {
if (this.get('location').pathname === this.getURL()) {
return;
}
window.location.href = this.getURL();
},
formatURL(url) {
return url;
},
getHash: Ember.Location._getHash
});
/initializers/location.js
import PassThroughLocation from '../location/passthrough';
export function initialize(instance) {
instance.register('location:passthrough', PassThroughLocation);
}
export default {
name: 'location',
initialize: initialize
};
Then in my router I have location:'passthrough'
set.
Part of my reasoning to do this is for certain pages I want to include additional modules and set separate CSP. Basically when you hit /secure/path
the server side sets a stricter CSP in the headers and loads a handful of modules only used in this section of the website.
UPDATE:
Overriding the behavior of a link-to
component seems possible although pretty intimate.
import Ember from 'ember';
export function initialize() {
Ember.LinkComponent.reopen({
_invoke: function (event) {
window.location.href = event.target.pathname;
return false;
}
});
}
export default {
name: 'location',
initialize: initialize
};