mardi 20 octobre 2015

Ember Force Route to Refresh Page

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
});




Aucun commentaire:

Enregistrer un commentaire