0

In a route, I would like my model ,where I called this.get('store'), to be dependent of a service. So when one of my value in the service changes, my model will be automatically refreshed.

I don't know if this is possible with a service and if it is a good practice.

services/current-contact.js

export default Service.extend({
  session: service('session'),
  store: service(),

  list() {
    if (this.get('session.isAuthenticated')) {
      return this.get('store').findAll('contrat').then((contrats) => {
        this.set('contrats', contrats);
      });
    } else {
      return RSVP.resolve();
    }
  },

  load() {
    if (this.get('session.isAuthenticated')) {
      if (localStorage.getItem('contrat')) {
        return this.get('store').find('contrat', JSON.parse(localStorage.getItem('contrat'))).then((contrat) => {
          this.set('contrat', contrat);
        });
      } else {
        return this.get('store').findAll('contrat').then((contrats) => {
          this.set('contrat', contrats.get('firstObject'));
          localStorage.setItem('contrat', JSON.stringify(contrats.get('firstObject').id));
        });
      }
    } else {
      return RSVP.resolve();
    }
  },

  select(id) {
    if (this.get('session.isAuthenticated')) {
      return this.get('store').findRecord('contrat', id).then((contrat) => {
        localStorage.setItem('contrat', JSON.stringify(contrat.id));
        window.location.reload(true);
      })
    } else {
        return RSVP.resolve();
      }
    }
});

my route

export default Route.extend(AuthenticatedRouteMixin, {
  session: service('session'),
  currentContract: service('current-contract'),

  model() {
    var self = this;
    return RSVP.hash({
      pdccontrats: this.get('store').findAll('pdccontrat').then(function(i) {
        return i.filterBy("contrat_fk.id", self.get('currentContract.contrat.id'));
      }),
      documentgroupefacturationclient: this.get('store').findAll('documentgroupefacturationclient'),
    })
  }
});
El Poisen
  • 121
  • 1
  • 8
  • 2
    If the service provides the model, it can refresh it's content when desired. Would need a lot more detail about what you are trying to do specifically though. – runspired Apr 13 '18 at 20:38
  • HI, sorry about the lack of code in my post, I updated it. – El Poisen Apr 16 '18 at 07:15

0 Answers0