【问题标题】:Ember JS: Nested api routesEmber JS:嵌套的 api 路由
【发布时间】:2016-01-10 04:35:55
【问题描述】:

我需要对以下 HTTP 端点执行 GET 操作:

/api/v1/users/security_questions.json?memberID=1234

有人告诉我 Ember JS 不支持像 users/security_questions 这样的嵌套端点,而我需要这样做:

/api/v1/security_questions.json?memberID=1234

是否支持嵌套 API 路由?如果是这样,我如何在我的模型中实现它?

【问题讨论】:

  • 如果您在users 有效负载中包含security_questions 链接,则可以。你考虑过普通的 AJAX 调用吗?

标签: javascript ember.js coffeescript ember-data


【解决方案1】:
  1. 要自定义任何端点,您必须为该模型编写适配器。
  2. 适配器中有多个可用的钩子。

    • host - 变量,在调用中作为前缀
    • pathForType(modelName) :可用于自定义给定类型的路径
    • buildURL (modelName, id, snapshot, requestType, query) :提供更多定制化

您可以在 Ember 指南中引用 here

【讨论】:

    【解决方案2】:

    不支持开箱即用,但添加它很容易。您需要覆盖 RESTAdapter 中的 pathForType 方法

    import Ember from 'ember';
    
    export default DS.RESTAdapter.extend({
    
      pathForType: function(type) {
    
        // Assuming your model name is security_question
        if (type === 'security_question' ) {
    
          // If it's a security question, set this as the path
          return 'users/security_questions.json';
    
        } else {
    
          // If not, call super to pluralize the model name as normal
          return this._super(type);
    
        }
    
      }
    
    });
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多