dimanche 14 mai 2017

How to modify a service property from controller in EmberJS?

I have a permissions service that has a session service injected within it.

export default Ember.Service.extend({

  session: inject.service(),
  store: inject.service(),

  data: computed('session.isAuthenticated', 'session.data', function() {
    if (get(this, 'session.isAuthenticated')) {
      return get(this, 'session.data.authenticated.userPermissions');
    }
    return null;
  }),

In a controller, I would like to modify the session.data value to cause it to recompute but I am not sure how to do this. The use case is, you hit a button with the action requestApi to update the data value.

export default Controller.extend({

  store: inject.service(),
  permissions: inject.service(),
  ajax: inject.service(),
  session: inject.service(),

  actions: {
    requestApi() {
      get(this, 'ajax')
        .request('/admin/session-data', {
          type: 'get'
        }).then(function(data) {
          //unsure how to use the data I have just received to replace the service.data
        });
    },

Basically, I don't know how to modify the session object file from within the promise function.




Aucun commentaire:

Enregistrer un commentaire