如何从 EmberJS 中的另一个 REST url 检索相同的模型

How to retrieve the same model from another REST url in EmberJS

我的 emberJS 应用程序的模型树与我的 API 的树相匹配,但是,我有 2 条不同的路由 returning 相同类型的数据:

/products//users/:id/supported_products/ 都是 return 产品数据。

当我需要 products 应用程序时没有问题:

 this.store.query('product',params);

但是我不确定如何从用户路径查询产品。这样做的地方是适配器,但我需要定义一个辅助适配器,当我需要支持的产品时我会调用它,但我不知道该怎么做。

我想如果是我我会创建一个 虚拟 查询参数来指示自定义适配器如何动态更改端点。

例如,我可能有一个 supportedByUser 标志。然后在我的 app/adapters/product.js 中做这样的事情:

import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default JSONAPIAdapter.extend({
  urlForQuery(query, modelName) {
    let userId = query.supportedByUser;
    delete query.supportedByUser;
    return userId
      ? `${this.namespace || ''}/users/${userId}/supported_products`
      : this._super(...arguments);
  }
});

这里有一个旋转演示示例:https://ember-twiddle.com/b406391e98ed4fda30bc227a894fa7c9