如何将查询参数传递给 DS 发出的请求.RestAdapter#findHasMany.



我有视频模型,我返回links参数,该参数指向来自 API 的 JSON 响应中的 related URL:

video: {
  id: 1,
  name: "Whatever",
  links: {
    related: "/videos/1/related"
  }
}

并在Video模型中具有related: hasMany('video', {async: true, inverse: null})关联。调用video.get('related')会向/videos/:id/related发出请求。这部分工作正常。

如何将查询参数传递给 ajax 请求以添加分页参数?我想提出像/videos/:id/related?per=3&page=5这样的请求。

遇到这个问题,看到这个问题仍然没有答案 - 不确定原始海报是否找到了解决方案。这样做的方法是覆盖 RESTAdapter 的 ajax 调用 - 我相信会有更好的方法来处理分页,但这是发送其他参数需要做的事情。

App.VideoAdapter = DS.RESTAdapter.extend({
    host: HOST,
    // Overriding ajax request to include account_id as a parameter
    ajax: function(url, type, hash) {
        if (Ember.isEmpty(hash)) hash = {};
        if (Ember.isEmpty(hash.data)) hash.data = {};
        hash.data.per = App.per; // Global variable - Not the smartest thing to do and should do something else
        hash.data.page = App.page; // Global variable - Not the smartest thing to do and should do something else
        return this._super(url, type, hash);
    }
});

最新更新