简体   繁体   English

向 Ember 适配器添加动态变量?

[英]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.有一个 EmberJS 问题,我一直在努力解决我希望有人能解释一下。

I have an application that has 2 principal root paths - one for tenants and one for internal admins.我有一个具有 2 个主要根路径的应用程序 - 一个用于租户,另一个用于内部管理员。

I'm setting up separate adapters in Ember to handle different namespacing, pathing.我正在 Ember 中设置单独的适配器来处理不同的命名空间、路径。 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.问题是,主要租户路由在分支到特定资源之前包含租户的 id,所以我希望该 id 包含在适配器的路径中,但无论我如何尝试,我似乎都无法当我在适配器中定义它时,让 Ember 识别它。

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 .我有一个可以从app_root/services/auth.js获取租户 ID 的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?我想知道是否可以在 Ember 适配器中添加动态段? If so, how to make it work?如果是这样,如何使其工作?

Thanks in advance,提前致谢,

You are already using a service into this adapter.您已经在此适配器中使用了一项服务。

Did you tried something like that :你有没有尝试过这样的事情:

( auth could be a very common name, already existing in some other packages, so prefer a more 'personal' name, here => 'tenant') auth可能是一个很常见的名字,已经存在于其他一些包中,所以更喜欢一个更“个人”的名字,这里=>“租户”)

app_root/services/tenant.js : app_root/services/tenant.js

import Ember from 'ember';

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

and your adapter :和你的适配器:

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({
  tenant: Ember.inject.service(),
  // ...


  namespace: computed(/*'tenant.id'*/, function() {
    return `${ENV.version}/tenant/${this.get('tenant').getTenantId()}/`;
  }),

  // ...
});

PS : if you import a service my_service into the key my_service , you don't have to specify your service name: PS:如果您将服务my_service导入密钥my_service ,则不必指定您的服务名称:

tenant: Ember.inject.service()
// is equivalent to
tenant: Ember.inject.service('tenant')

// while
my_tenant_service: Ember.inject.service('tenant') // import 'tenant' service into 'my_tenant_service' key
// isn't equivalent to
my_tenant_service: Ember.inject.service() // import 'my_tenant_service' service into 'my_tenant_service' key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM