lundi 27 août 2018

Adding a dynamic variable to an Ember adapter?

have a EmberJS problem I've been struggling with I'm hoping someone can shed some light on.

I have an application that has 2 principal root paths - one for tenants and one for internal admins.

I'm setting up separate adapters in Ember to handle different namespacing, pathing. The problem is, the principal tenant route includes an id for the tenant before branching into specific resources, so I'd like that id to be included in the path for the adapter, but no matter how I try, I can't seem to get Ember to recognize the id when I define it in the adapter.

The internal admin adapter doesn't have this problem, as the path prior to the resource branching is static.

This is the adapter I have:

import Ember from 'ember';
import { pluralize } from 'ember-inflector';
import { underscore } from 'ember-string';
import config from 'config/environment';
import DS from 'ember-data';
import ENV from 'config/environment';



export default DS.JSONAPIAdapter.extend({
  auth: Ember.inject.service('auth'),

  headers: Ember.computed('auth.authToken', function() {
    return {
      "Authorization": `Bearer ${this.get("auth").getAuthToken('user')}`,
    };
  }), 

  host: config.apiHost,
  namespace: ENV.version,

  pathForType: function(type) {
    var underscored = underscore(type);
    return pluralize(underscored);
  },

});

What I'd like to have there is more like:

ENV.version + '/tenant/' + :tenant_id + '/',

But I can't seem to get that to work. I have a service I can grab the tenant id from: app_root/services/auth.js. It looks something like:

import Ember from 'ember';

export default Ember.Service.extend({
    getTenantId(){
        return '1234';
    },
});

Am I allowed to import the service into the adapter? I tried creating a function in the adapter that would call this service and return the value from it but it fails every time. I also tried importing the service but that failed as well. I'm wondering is it even possible to add a dynamic segment in an Ember adapter? If so, how to make it work?

Thanks in advance,




Aucun commentaire:

Enregistrer un commentaire