0

How do I get fresh data from the server/DS in ember without reloading a page?

here is the updated component for ember

 import Component from '@ember/component';
import { set } from '@ember/object';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';

export default Component.extend({
  router: service(),


  init() {
    this._super(...arguments);
    let alertColor = this.alertColor;
  this.refreshInterval = setInterval(() => {
      if (this.isDestroyed || this.isDestroying) return;

      this.router.refresh();
    }, 3_000);
  }

 })

this is the route... I think. There are other ones but this I think is the one it uses. Im not positive though. Theres also a component which i deleted because Stackoverflow said there wasnt enough details and mostly code...so thats why Im adding all this text here

import AddThis from '../mixins/add-this';
import getQueryVariable from '../utils/get-query';
import Route from '@ember/routing/route';
import { get } from '@ember/object';

export default Route.extend(AddThis, {
  model: function(args) {
    this._super(...arguments);

    return this.store.findRecord('landing-page', args.id);
  },

  setupController: function(controller, model) {
    this._super(...arguments);
    const  href = get(model, 'uri') +
                  window.location.search +
                  window.location.hash,
           id = getQueryVariable('id');

    if (id && !isNaN(id) && get(model, 'cardPageType') === '2') {
      this.transitionTo('image', get(model, 'id'), 0, id);
    }

    // @TODO Not 100% clear on what this is for
    if (get(model, 'uri')) {
      window.history.replaceState('', get(model, 'title'), href);
    }
  },

  actions: {
    goToVideo: function(id) {
      this.transitionTo('video', id);
    },
  },
});
14
  • that's not a model, that's a component. how are you fetching data? Commented Mar 10, 2022 at 16:02
  • 1
    hold on Ill add the model Commented Mar 10, 2022 at 16:09
  • how are you initially fetching the data? Commented Mar 10, 2022 at 16:21
  • I dont know...would that be in routes? Commented Mar 10, 2022 at 16:28
  • 1
    also, feel free to ignore me, if you know what you're doing, but it seems like you're struggling to understand how the different pieces of the framework interact with each other. I highly recommend reading through the official tutorial: guides.emberjs.com/release/tutorial/part-1 It is top-notch! Commented Mar 11, 2022 at 18:02

1 Answer 1

0

Since your loading data in the landing-page route, you can use the RouterService to refresh the route.

For example, whenever you want to refresh our data fetched from a route, you do the following:

import { service } from '@ember/service';
import Component from '@glimmer/component';

export default class Demo extends Component {
  @service router;

  refreshMe = () => {
    this.router.refresh('landing-page');
    // or to refresh all routes:
    // this.router.refresh();
  }
}
<button {{on 'click' this.refreshMe}}>
  Refresh Me
</button>

Note that prior to [email protected], you'll need to install this polyfill: https://github.com/Windvis/ember-router-service-refresh-polyfill (which supports all the way back to [email protected], which is > 3.5 years ago, at the time of writing)


edit: demonstrating how to use the legacy syntax and refreshing every 60s.

import { inject as service } from '@ember/service';

// ...
export default Component.extend({
  // ...
  router: service(),

  init() {
    this._super(...arguments);
    // ...

    
    this.refreshInterval = setInterval(() => {
      if (this.isDestroyed || this.isDestroying) return;

      this.router.refresh();
    }, 60_000);
  }

  willDestroy() {
    clearInterval(this.refreshInterval);
  }

Docs:

Sign up to request clarification or add additional context in comments.

3 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
youre saying that the model should be updating though once the router is refreshed?
that is correct, yeah

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.