store.find() 以关系对象作为参数?

store.find() with relationship-object as parameter?

我有两个模型 Post.jsCategory.js

Post.js

import DS from 'ember-data';

var Post = DS.Model.extend({
  title: DS.attr('string'),
  permalink: DS.attr('string'),
  body: DS.attr('string'),
  category: DS.belongsTo('category')
});

export default Post;

Category.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  permalink: DS.attr('string'),
  posts: DS.hasMany('post',{async:true})
});

我想通过以下方式检索属于某个类别的所有帖子 http://localhost:4200/<category_permalink>.

我无法检索路线中的数据,因为 category.permalink

这不起作用

路线

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', { category.permalink: params.category });
  }
});

首先,{ category.permalink: params.category } 不是有效的 JSON。您可能想要实现的是拥有这样一个端点:

GET http://localhost:4200/api/post?category_permalink=:category_permalink`

那么,在 Ember 中,您更愿意使用:

this.store.find('post', {
  category_permalink: params.category
});

来自服务器的响应应该大致如下所示:

{
  "post": [{
    "id": 1,
    "title": "title",
    "body": "body",
    "permalink": "permalink",
    "category": 10
  }],
  "category": {
    "id": 10,
    "name": "name",
    "permalink": "c_permalink",
    "posts": [1,2,3]
  }
}